¿Cuál es la forma correcta para que un script bash agregue un trabajo a crontab, de modo que
Encontré esta solución a continuación, pero no afecta el resultado de ejecutar crontab -l .
grep 'some_user python /mount/share/script.py' /etc/crontab || echo '*/1 * * * * some_user python /mount/share/script.py' >> /etc/crontab Intenté convertirlo para afectar 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 -pero ejecutar este comando da el error:
-bash: */1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1: No such file or directorypero ejecutar este comando da el error:
-bash: */1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1: No such file or directoryEl código:
(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 -intentará ejecutar/ejecutar:
'*/1 * * * * some_user python /mount/share/script.py >> /root/foo/logs/foo.cron.log 2>&1' Si y cuando grep falló.
Agregue un eco delante de él o printf, ya que crontab espera una entrada de stdin , como lo que hizo en su primer ejemplo/código, algo como:
(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 -Aquí hay una alternativa, que es un script completo.
#!/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