How to create a cron job using Bash automatically

2019-01-03 11:30发布

Does crontab have an argument for creating cron jobs without using the editor (crontab -e). If so, What would be the code create a cronjob from a Bash script?

标签: bash shell cron
18条回答
时光不老,我们不散
2楼-- · 2019-01-03 11:44

This shorter one requires no temporary file, it is immune to multiple insertions, and it lets you change the schedule of an existing entry.

Say you have these:

croncmd="/home/me/myfunction myargs > /home/me/myfunction.log 2>&1"
cronjob="0 */15 * * * $croncmd"

To add it to the crontab, with no duplication:

( crontab -l | grep -v -F "$croncmd" ; echo "$cronjob" ) | crontab -

To remove it from the crontab whatever its current schedule:

( crontab -l | grep -v -F "$croncmd" ) | crontab -

Notes:

  • grep -F matches the string literally, as we do not want to interpret it as a regular expression
  • We also ignore the time scheduling and only look for the command. This way; the schedule can be changed without the risk of adding a new line to the crontab
查看更多
太酷不给撩
3楼-- · 2019-01-03 11:44

If you're using the Vixie Cron, e.g. on most Linux distributions, you can just put a file in /etc/cron.d with the individual cronjob.

This only works for root of course. If your system supports this you should see several examples in there. (Note the username included in the line, in the same syntax as the old /etc/crontab)

It's a sad misfeature in cron that there is no way to handle this as a regular user, and that so many cron implementations have no way at all to handle this.

查看更多
forever°为你锁心
4楼-- · 2019-01-03 11:48

So, in Debian, Ubuntu, and many similar Debian based distros...

There is a cron task concatenation mechanism that takes a config file, bundles them up and adds them to your cron service running.

You can put a file under the /etc/cron.d/somefilename where somefilename is whatever you want.

sudo echo "0,15,30,45 * * * * ntpdate -u time.nist.gov" >> /etc/cron.d/vmclocksync

Let's disassemble this:

sudo - because you need elevated privileges to change cron configs under the /etc directory

echo - a vehicle to create output on std out. printf, cat... would work as well

" - use a doublequote at the beginning of your string, you're a professional

0,15,30,45 * * * * - the standard cron run schedule, this one runs every 15 minutes

ntpdate -u time.nist.gov - the actual command I want to run

" - because my first double quotes needs a buddy to close the line being output

>> - the double redirect appends instead of overwrites*

/etc/cron.d/vmclocksync - vmclocksync is the filename I've chosen, it goes in /etc/cron.d/


* if we used the > redirect, we could guarantee we only had one task entry. But, we would be at risk of blowing away any other rules in an existing file. You can decide for yourself if possible destruction with > is right or possible duplicates with >> are for you. Alternatively, you could do something convoluted or involved to check if the file name exists, if there is anything in it, and whether you are adding any kind of duplicate-- but, I have stuff to do and I can't do that for you right now.

查看更多
霸刀☆藐视天下
5楼-- · 2019-01-03 11:50

A variant which only edits crontab if the desired string is not found there:

CMD="/sbin/modprobe fcpci"
JOB="@reboot $CMD"
TMPC="mycron"
grep "$CMD" -q <(crontab -l) || (crontab -l>"$TMPC"; echo "$JOB">>"$TMPC"; crontab "$TMPC")
查看更多
Viruses.
6楼-- · 2019-01-03 11:54
CRON="1 2 3 4 5 /root/bin/backup.sh" 
cat < (crontab -l) |grep -v "${CRON}" < (echo "${CRON}")

add -w parameter to grep exact command, without -w parameter adding the cronjob "testing" cause deletion of cron job "testing123"

script function to add/remove cronjobs. no duplication entries :

cronjob_editor () {         
# usage: cronjob_editor '<interval>' '<command>' <add|remove>

if [[ -z "$1" ]] ;then printf " no interval specified\n" ;fi
if [[ -z "$2" ]] ;then printf " no command specified\n" ;fi
if [[ -z "$3" ]] ;then printf " no action specified\n" ;fi

if [[ "$3" == add ]] ;then
    # add cronjob, no duplication:
    ( crontab -l | grep -v -F -w "$2" ; echo "$1 $2" ) | crontab -
elif [[ "$3" == remove ]] ;then
    # remove cronjob:
    ( crontab -l | grep -v -F -w "$2" ) | crontab -
fi 
} 
cronjob_editor "$1" "$2" "$3"

tested :

$ ./cronjob_editor.sh '*/10 * * * *' 'echo "this is a test" > export_file' add
$ crontab  -l
$ */10 * * * * echo "this is a test" > export_file
查看更多
乱世女痞
7楼-- · 2019-01-03 11:57

You may be able to do it on-the-fly

crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -

crontab -l lists the current crontab jobs, cat prints it, echo prints the new command and crontab - adds all the printed stuff into the crontab file. You can see the effect by doing a new crontab -l.

查看更多
登录 后发表回答