Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert entry into crontab unless it already exists (as one-liner if possible) [duplicate]

Tags:

bash

centos6

What's the preferred method to insert an entry into /etc/crontab unless it exists, preferably using a one-liner?

Here's my example entry I wish to place into /etc/crontab unless it already exists in there.

*/1 *  *  *  * some_user python /mount/share/script.py

I'm on CentOS 6.6 and so far I have this:

if grep "*/1 *  *  *  * some_user python /mount/share/script.py" /etc/crontab; then echo "Entry already in crontab"; else echo "*/1 *  *  *  * some_user python /mount/share/script.py" >> /etc/crontab; fi 
like image 548
fredrik Avatar asked Dec 01 '14 11:12

fredrik


People also ask

What does 2 mean in crontab?

so */2 means every other hour, */3 every third hour, etc. The default step is 1, so you can omit /1 if you want a step value of 1. see the crontab(5) man page for more detail.

How do I check if crontab exists?

To verify that a crontab file exists for a user, use the ls -l command in the /var/spool/cron/crontabs directory.


1 Answers

You can do this:

grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 *  *  *  * some_user python /mount/share/script.py' >> /etc/crontab

If the line is absent, grep will return 1, so the right hand side of the or || will be executed.

like image 128
arco444 Avatar answered Dec 31 '22 15:12

arco444