添加到 Crontab(如果尚不存在)使用 Bash 脚本
Add To Crontab (if not already exists) Using Bash Script
bash 脚本将作业添加到 crontab 的正确方法是什么
- 不会有重复的工作
- crontab 文件不会被调用
- (可选)接近单线
在下面遇到了这个解决方案,但它不影响 运行 crontab -l
.
的输出
grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 * * * * some_user python /mount/share/script.py' >> /etc/crontab
尝试将其转换为影响 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 -
但是运行这个命令给出了错误:
-bash: */1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1: No such file or directory
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
代码:
(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 -
将尝试execute/run:
'*/1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1'
如果 grep
失败。
在它或 printf 前面添加一个 echo,因为 crontab
期待来自 stdin
的输入,就像您在第一个 example/code 上所做的一样,例如:
(crontab -l | grep '/mount/share/script.py') || { crontab -l; echo '*/1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1'; } | crontab -
这是一个替代方案,它是一个完整的脚本。
#!/usr/bin/env bash
cron_entry=$(crontab -l 2>&1)
is_in_cron='/mount/share/script.py'
new_cron_entry='*/1 * * * * some_user python /mount/share/script.py >> /tmp/foo/logs/foo.cron.log 2>&1'
if [[ $cron_entry != *"$is_in_cron"* ]]; then
printf '%s\n' "$cron_entry" "$new_cron_entry" | crontab -
fi
bash 脚本将作业添加到 crontab 的正确方法是什么
- 不会有重复的工作
- crontab 文件不会被调用
- (可选)接近单线
在下面遇到了这个解决方案,但它不影响 运行 crontab -l
.
grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 * * * * some_user python /mount/share/script.py' >> /etc/crontab
尝试将其转换为影响 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 -
但是运行这个命令给出了错误:
-bash: */1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1: No such file or directory
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
代码:
(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 -
将尝试execute/run:
'*/1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1'
如果 grep
失败。
在它或 printf 前面添加一个 echo,因为 crontab
期待来自 stdin
的输入,就像您在第一个 example/code 上所做的一样,例如:
(crontab -l | grep '/mount/share/script.py') || { crontab -l; echo '*/1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1'; } | crontab -
这是一个替代方案,它是一个完整的脚本。
#!/usr/bin/env bash
cron_entry=$(crontab -l 2>&1)
is_in_cron='/mount/share/script.py'
new_cron_entry='*/1 * * * * some_user python /mount/share/script.py >> /tmp/foo/logs/foo.cron.log 2>&1'
if [[ $cron_entry != *"$is_in_cron"* ]]; then
printf '%s\n' "$cron_entry" "$new_cron_entry" | crontab -
fi