-1

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 /home/user1/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

  • 1
    Use absolute path for all the executables in your script, or define/set the PATH via cron entry. – Jetchisel Nov 01 '21 at 06:40
  • @Jetchisel : I don't think that setting `PATH` would have an effect on starting the script, because if a program is started by giving a relative path, the PATH variable is not used. Of course an incorrect setting of PATH may affect what happens _inside_ the invoked shell script. – user1934428 Nov 01 '21 at 07:52
  • 1
    See: [Mysqldump launched by cron and password security](https://stackoverflow.com/q/6861355/3776858) – Cyrus Nov 01 '21 at 07:55
  • @user1934428, Right I forgot to add that if you set the PATH then don't use the `./` for the executable. – Jetchisel Nov 01 '21 at 07:57
  • @user1934428 Both the scripts are in same directory. I've also tried setting full path. it doesn't run with cronjob. But it runs successfully when executing from terminal. – Nitin Kumar Nov 01 '21 at 08:37
  • try adding `set env(TERM) linux` before `spawn`. – sexpect - Expect for Shells Nov 01 '21 at 12:25

1 Answers1

0

The working directory of the script does not contain a sql_backup.sh. Do a

cd /home/user1

(or whereever your Shell script is stored). See here for the Tcl cd command.

user1934428
  • 19,864
  • 7
  • 42
  • 87
  • Both the scripts are in same directory. I've also tried setting full path. it doesn't run with cronjob. But it runs successfully when executing from terminal. – Nitin Kumar Nov 01 '21 at 08:33
  • It is not relevant whether or not the scripts are in the same directory. I said that you need to make sure that the **working directory** is set correctly. – user1934428 Nov 01 '21 at 10:48
  • @NitinKumar: And, if you did place your full path in the script, update your question so that one can see, how you did it. Finally, _doesn't run_ is an unusuable error description. What does it mean? If things go wrong, you usually get some error message. I don't see in your posting that you would collect the error logs from your cron job. How are you then going to debug the problem? – user1934428 Nov 01 '21 at 10:50
  • I don't see any error in logs Nov 1 11:10:01 bitsify CRON[102105]: (root) CMD (/usr/bin/expect -f /home/user1/script.exp) Nov 1 11:10:01 bitsify CRON[102104]: (CRON) info (No MTA installed, discarding output) Nov 1 11:11:01 bitsify CRON[102111]: (root) CMD (/usr/bin/expect -f /home/user1/script.exp) Nov 1 11:11:01 bitsify CRON[102110]: (CRON) info (No MTA installed, discarding output) Nov 1 11:12:01 bitsify CRON[102121]: (root) CMD (/usr/bin/expect -f /home/user1/script.exp) Nov 1 11:12:02 bitsify CRON[102119]: (CRON) info (No MTA installed, discarding output) – Nitin Kumar Nov 01 '21 at 11:19
  • @NitinKumar change your crontab to `* * * * * /usr/bin/expect -f /home/user1/script.exp > /tmp/crontab.log 2>&1` You should be able to see some messages in /tmp/crontab.log – Philippe Nov 01 '21 at 11:36
  • @NitinKumar : Don't place such information into a comment. Put it nicely formatted into your question. Note that you can *edit* your question, for providing additiional information. BTW - AFIK, cron sends you the stderr of the jobs by email, so you won't find it in the logs anyway. – user1934428 Nov 02 '21 at 07:45