Bash
不確定是否執行 cron 作業
我有以下 cron 工作
/etc/cron.d/backup
:*/1 * * * * backupbot /home/backupbot/bin/backup-script.sh
基本上,我希望
backup-script.sh
每分鐘執行一次(並且使用者備份機器人應該正在執行該作業)。該
/home/backupbot/bin/backup-script.sh
文件歸backupbot所有(他擁有“x”權限)。該文件如下所示:#!/bin/bash set -e { BACKUP_DIR=/var/app/backups STORAGE_ACCOUNT_URL=https://myserver/backups BACKUP_FILE=$(ls $BACKUP_DIR -t | head -1) if [ -z "$BACKUP_FILE" ]; then echo "There are no backups to synchronize" exit 0 fi azcopy login --identity azcopy copy $BACKUP_DIR/$BACKUP_FILE $STORAGE_ACCOUNT_URL/$BACKUP_FILE } >/tmp/cron.backup-script.$$ 2>&1
通常,任何輸出都應登錄到
/tmp/cron.backup-script.xxxx
. 永遠不會創建這樣的文件。Cron 注意到該作業的唯一證據是以下輸出
systemctl status cron.service
:● cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled) Active: active (running) since Tue 2021-04-13 09:59:08 UTC; 6h ago Docs: man:cron(8) Main PID: 1086 (cron) Tasks: 1 (limit: 4915) CGroup: /system.slice/cron.service └─1086 /usr/sbin/cron -f Apr 13 16:00:01 my-vm CRON[17201]: pam_unix(cron:session): session closed for user root Apr 13 16:00:01 my-vm CRON[17198]: pam_unix(cron:session): session closed for user root Apr 13 16:00:01 my-vm CRON[17199]: pam_unix(cron:session): session closed for user root Apr 13 16:01:01 my-vm CRON[17402]: pam_unix(cron:session): session opened for user root by (uid=0) Apr 13 16:01:01 my-vm CRON[17403]: (root) CMD ([ -f /etc/krb5.keytab ] && [ \( ! -f /etc/opt/omi/creds/omi.keytab \) -o \( /etc/krb5.keytab -nt /etc/opt/omi/creds/omi.keytab \) ] && Apr 13 16:01:01 my-vm CRON[17401]: pam_unix(cron:session): session opened for user backupbot by (uid=0) Apr 13 16:01:01 my-vm CRON[17404]: (backupbot) CMD (/home/backupbot/bin/backup-script.sh) Apr 13 16:01:01 my-vm CRON[17402]: pam_unix(cron:session): session closed for user root Apr 13 16:01:01 my-vm CRON[17401]: (CRON) info (No MTA installed, discarding output) Apr 13 16:01:01 my-vm CRON[17401]: pam_unix(cron:session): session closed for user backupbot
它提到了有關backupbot的會話。我該如何進一步調查?
根據評論,該腳本只能由所有者執行。即,它不可讀。這會阻止所有者執行腳本。
例子:
$ chmod 500 script $ ls -l script -r-x------ 1 myself myself 24 Apr 14 09:21 script $ ./script hello
$ chmod 100 script $ ls -l script ---x------ 1 myself myself 24 Apr 14 09:21 script $ ./script /bin/bash: ./script: Permission denied
bash
如果腳本文件不可讀,則shell 解釋器(以目前使用者身份執行)無法讀取它。使腳本既可執行又可讀。
除了上述之外,考慮使用適當的雙引號變數擴展,並且不要解析輸出
ls
(它只是真正有用的看)。您的腳本,已修改:
#!/bin/bash backup_dir=/var/app/backups storage_account_url=https://myserver/backups { # Find most recently modified file in "$backup_dir". # Assumes that there are only files there, no subdirectories. set -- "$backup_dir"/* backup_file_path=$1 shift for pathname do if [ "$pathname" -nt "$backup_file_path" ]; then backup_file_path=$pathname fi done if [ ! -e "$backup_file_path" ]; then echo 'There are no backups to synchronize' exit 1 fi # Perform backup. azcopy login --identity || exit 1 azcopy copy "$backup_file_path" "$storage_account_url/$(basename "$backup_file_path")" } >/tmp/cron.backup-script.$$ 2>&1