将条目插入到crontab中,除非它已经存在(如果可能的话,作为单行代码)

fre*_*rik 8 bash centos6

将条目插入/ etc/crontab的首选方法是什么,除非它存在,最好使用单行?

这是我希望放入/ etc/crontab的示例条目,除非它已存在于那里.

*/1 *  *  *  * some_user python /mount/share/script.py
Run Code Online (Sandbox Code Playgroud)

我在CentOS 6.6上,到目前为止,我有这个:

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 
Run Code Online (Sandbox Code Playgroud)

arc*_*444 16

你可以这样做:

grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 *  *  *  * some_user python /mount/share/script.py' >> /etc/crontab
Run Code Online (Sandbox Code Playgroud)

如果该行不存在,grep将返回1,因此该权利的右侧||将被执行.

  • (crontab -l | grep 'some_user python /mount/share/script.py') || { crontab -l; '*/1 * * * * some_user python /mount/share/script.py'; } | crontab - (3认同)
  • 我知道这些天我们不应该直接编辑/ etc / crontab-我们将如何使用crontab -e进行编辑? (2认同)