除非条目已经存在,否则将其插入crontab中(如果可能,则为单行) [英] Insert entry into crontab unless it already exists (as one-liner if possible)

查看:108
本文介绍了除非条目已经存在,否则将其插入crontab中(如果可能,则为单行)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 


推荐答案

您可以执行以下操作:

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

如果该行不存在,则 grep 将返回 1 ,因此将执行or或 || 的右侧。

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

这篇关于除非条目已经存在,否则将其插入crontab中(如果可能,则为单行)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆