What is the correct way for a bash script to add a job to crontab, such that
- there will be no duplicate jobs
- the crontab file will not be resorted
- (optional) close to being a one-liner
Came across this solution below, but it does not affect the output of running crontab -l
.
grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 * * * * some_user python /mount/share/script.py' >> /etc/crontab
Tried converting it to affect crontab -l
,
(crontab -l | grep '/mount/share/script.py') || { crontab -l; '*/1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1'; } | crontab -
but running this command gives the error:
-bash: */1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1: No such file or directory