regenpfeifer schrieb:
| (crontab -l 2>/dev/null; echo "*/5 * * * * /path/to/job -with args") | crontab -
|
Zu einen weiß ich aber leider nicht genau, was dieser Befehl wirklich macht,
Er liest die aktuelle Crontab aus, gibt eine weitere Zeile aus (den neuen Eintrag) und sendet beides verkettet wieder zu crontab, was letztlich die Crontab aktualisiert.
zum anderen scheint hier der Eintrag nicht für den Crontab eines bestimmten Benutzers zu erfolgen (in meinem Fall www-data). Jedenfalls scheint es mit
| (crontab -u www-data -l 2>/dev/null; echo "*/5 * * * * /path/to/job -with args") | crontab -
|
nicht zu funktionieren.
Ja, weil beim letzten crontab in der Zeile die Option "-u" nebst Angabe des Nutzers fehlt. Die runden Klammern sind übrigens überflüssig.
Hat jemand einen Hinweis und / oder Verbesserungsvorschlag? Danke!
Der Ansatz funktioniert, wird aber bei jedem Aufruf einen neuen Eintrag in der Crontab machen. Das will man normalerweise nicht. Am besten fügt man eine Markierung ein, an der man erkennt, dass bereits ein Eintrag da ist. Zur Illustration (ohne crontab):
1
2
3
4
5
6
7
8
9
10
11
12
13 | $ touch demo
$ wc demo
0 0 0 demo
$ { sed '/^# DEMO/,+1d' demo; echo -e '# DEMO: automatic execution of X\n*/5 * * * * /path/to/job -with args'; } >| demo.tmp
$ mv -f demo.tmp demo
$ cat demo
# DEMO: automatic execution of X
*/5 * * * * /path/to/job -with args
$ { sed '/^# DEMO/,+1d' demo; echo -e '# DEMO: automatic execution of X\n*/5 * * * * /path/to/job -with args new'; } >| demo.tmp
$ mv -f demo.tmp demo
$ cat demo
# DEMO: automatic execution of X
*/5 * * * * /path/to/job -with args new
|
Jetzt mal mit einem weiteren Eintrag:
| $ echo '* * * * * /bin/foo' >> demo
$ cat demo
# DEMO: automatic execution of X
*/5 * * * * /path/to/job -with args new
* * * * * /bin/foo
$ { sed '/^# DEMO/,+1d' demo; echo -e '# DEMO: automatic execution of X\n*/5 * * * * /path/to/job -with args new'; } >| demo.tmp
$ mv -f demo.tmp demo
$ cat demo
* * * * * /bin/foo
# DEMO: automatic execution of X
*/5 * * * * /path/to/job -with args new
|
Man sieht, der Eintrag von /bin/foo bleibt erhalten.