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
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.
To verify that a crontab file exists for a user, use the ls -l command in the /var/spool/cron/crontabs directory.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With