Running "ghost backup" automated in shell script with Expect // manually works, crontab doesn't

Recently I tried to use “ghost backup” in a shell script. The idea was to use it in combination with Expect to hand over the required ghost username/email and passwords automatically. As I like the small footprint of “ghost backup” compared to a full backup of /var/www/… I though this might be a good way to go (in combination with mysqldump backups). However, I noticed the following behavior that I struggle to understand:

When starting the Expect script (details below) manually from the shell (as the ghostblog user) everything works fine and I end up with the desired backup zip-file without any further interaction. When starting the same command in a cronjob of the same user, I end up with the following error:

Debug Information:
    OS: Ubuntu, v22.04.2 LTS
    Node Version: v18.17.0
    Ghost Version: 5.58.0
    Ghost-CLI Version: 1.24.2
    Environment: production
    Command: 'ghost backup'
An error occurred.
Message: 'Command failed: /bin/sh -c sudo -S -p '#node-sudo-passwd#' -E -u undefined mkdir -p /var/www/
sudo: unknown user undefined
sudo: error initializing audit plugin sudoers_audit

Stack: Error: Command failed: /bin/sh -c sudo -S -p '#node-sudo-passwd#' -E -u undefined mkdir -p /var/www/
sudo: unknown user undefined
sudo: error initializing audit plugin sudoers_audit

    at makeError (/usr/lib/node_modules/ghost-cli/node_modules/execa/index.js:174:9)
    at /usr/lib/node_modules/ghost-cli/node_modules/execa/index.js:278:16
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ensureBackupFolder (/usr/lib/node_modules/ghost-cli/lib/tasks/backup.js:17:9)
    at async module.exports (/usr/lib/node_modules/ghost-cli/lib/tasks/backup.js:57:5)
    at async /usr/lib/node_modules/ghost-cli/lib/commands/backup.js:35:26
Code: 1

It seems to me that starting the job via cron somehow results in an “undefined” variable that breaks the backup routine of ghost-CLI. Does anybody have an idea what the reason might be and can point me into the right direction?
Any help would be very much appreciated.


used Expect script <ghost_credentials.exp>:


set sudopw "mysudopassword123"
set email ""
set pw "adminatmylittesitepw123"

spawn ghost backup

expect "Sudo Password"
send "$sudopw\r"

expect "Ghost administrator email address"
send "$email\r"

expect "Ghost administrator password"
send "$pw\r"

expect eof

Command to call expect script (same used for manual and crontab)

cd /var/www/ && /usr/bin/expect /var/www/ > ~/backup.log
1 Like

Errors like this are usually caused by differences in environmental variables.

Use env in both shell and from contexts to see what’s different.

Look at values like PATH and CWD to see how they differ.


Hi markstos,

Thanks for the hint to use env in both contexts. This saved me hours of work!!

Using env in the shell showed a variable USER which was set and was absent when running env from a cronjob. Manually setting the variable in the crontab did the job.

Again thanks for your help!

crontab -e for reference:

15 10 * * * cd /var/www/ && /usr/bin/expect /var/www/ > ~/backup.log

It’s better to put USER=on the same line so it only affects that line and not all that follow it:

USER=myuser cd…

There are also user-specific crontab files, but those can be more confusing to manage if there’s just one human using the server.

1 Like