Score:0

expect script not working in cron but runs successfully in terminal

us flag

Here is my expect script named as script.exp which runs successfully when executed from terminal. But it doesn't run when scheduling through cronjob.

#!/usr/bin/expect -f
set timeout -1
spawn ./sql_backup.sh
match_max 100000
expect -exact "Enter password: "
send -- "pass123\r"
expect eof

my bash script named as sql_backup.sh is:

#!/bin/bash
mysqldump -u root -p --all-databases > /home/user1/mysql/mysql-bkp.sql

and my cronjob is:

* * * * * /usr/bin/expect -f /home/user1/script.exp

Thanks

cn flag
Duplicate of https://stackoverflow.com/q/69793254/7552 ? Please don't ask the same question in different places.
Score:1
it flag

Jobs run through cron aren't run in the same runtime environment that you have on your desktop. None of your PATH changes, or other environment variable settings from ~/.bashrc are automatically propagated to your cron job. For example, there's no $DISPLAY, so GUI programs need special treatment (read man xhost).

One can set environment variables for all one's cron jobs in the crontab file Read man 5 crontab.

Look at the results of echo "=== id ===";id;echo "=== set ===";set;echo "=== env ===";env | sort;echo "=== alias ===";alias in each of your environments.

Since the command part of the crontab line is, by default, interpreted by /bin/sh, which has a simpler syntax than /bin/bash, I recommend having command be a call to a bash script (executable, mounted, starts with #!/bin/bash) which sets up the environment, then calls the desired program.

cn flag
Also note the CWD for cron is `/`, so `./script.sh` is wrong.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.