我正在通过 cron 运行数据库备份脚本,并希望将程序的输出发送到系统日志(因此它会被发送到Stackdriver 日志)。
我一直在尝试运行以下命令:
/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 作业内部运行命令有些不同,但我找不到任何东西。
答案1
问题已解决:日期格式($(date +"%Y-%m-%d_%H-%M-%S")
)使用了%
符号,该符号由 cron 保留。
替换%
后\%
问题得到解决。