Cron

到 systemd-cat 或 logger 的管道在 cron 作業中不起作用

  • January 23, 2018

我正在通過 cron 執行數據庫備份腳本,並希望將程序的輸出發送到系統日誌(因此它被發送到Stackdriver Logging)。

我一直在嘗試執行以下內容:

/opt/orientdb/bin/backup.sh \
 remote:localhost/[database name] \
 root root \
 /db-backup/orientdb-dev/$(date +"%Y-%m-%d_%H-%M-%S").zip \
 lvm \
 2>&1 \
| tr '\n' ' ' \
| /usr/bin/systemd-cat -t "orientdb-backup"

但是,此腳本在直接呼叫時會輸出到 syslog,但在通過 cron 執行時不包含任何輸出。

手動執行後的 Syslog(刪除了不相關的行):

[user]@orientdb-dev:~$ sudo /opt/orientdb/bin/backup.sh remote:localhost/[database name] root root /db-backup/orientdb-dev/$(date +"%Y-%m-%d_%H-%M-%S").zip lvm 2>&1 | tr '\n' ' ' | /usr/bin/systemd-cat -t "orientdb-backup"
[user]@orientdb-dev:~$ tail /var/log/syslog
Jan 21 18:16:48 orientdb-dev orientdb-backup[3522]: /opt/orientdb /opt/orientdb/bin/backup.sh: 103: cd: can't cd to /opt/orientdb/databases/[database name]   Volume group "sda1" not found   Cannot process volume group sda1  2018-01-21 18:16:47:716 WARNING No enough physical memory available for DISKCACHE: 581MB (heap=494MB). Set lower Maximum Heap (-Xmx setting on JVM) and restart OrientDB. Now running with DISKCACHE=256MB [orientechnologies] Error: com.orientechnologies.orient.core.exception.OStorageException: Cannot create a connection to remote server address(es): [127.0.0.1:2424]  ERROR # 1 : database freeze failed

cron 文件:

$ cat /etc/cron.d/orientdb-backup 
#Ansible: backup
*/5 * * * * root /opt/orientdb/bin/backup.sh remote:localhost/[database name] root root /db-backup/orientdb-dev/$(date +"%Y-%m-%d_%H-%M-%S").zip lvm 2>&1 | tr '\n' ' ' | /usr/bin/systemd-cat -t "orientdb-backup"

cron 作業執行後的 syslog:

Jan 21 18:15:01 orientdb-dev CRON[3438]: (root) CMD (/opt/orientdb/bin/backup.sh remote:localhost/[database name] root root /db-backup/orientdb-dev/$(date +")
Jan 21 18:15:01 orientdb-dev CRON[3437]: (CRON) info (No MTA installed, discarding output)

感覺在 shell 上執行命令與在 cron 作業中執行命令之間存在一些不同,但我找不到任何東西。

問題解決:日期格式($(date +"%Y-%m-%d_%H-%M-%S"))使用了%cron保留的符號。

替換%\%已修復的問題。

引用自:https://serverfault.com/questions/893356