Useful information related to unix bash scripts

Check if a script is already running:

test "$(pidof -x "$(basename $0))" != $$ && exit
# or (bash only):
[[ "$(pidof -x "$(basename $0))" != $$ ]] && exit

Get the full directory name of the script no matter where it is being called from (src):

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Automatically manage cronjob additions and removal of commands:

croncmd="/home/me/myfunction start 2> /home/me/myfunction/cron_errors < /dev/null"
cronjob="0 0 * * * $croncmd"
# Add
( crontab -l | grep -v "$croncmd" ; echo "$cronjob" ) | crontab -
# Remove
( crontab -l | grep -v "$croncmd" ) | crontab -
linux/bash.txt · Last modified: 2014/07/16 18:51 by jim
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki