Why isn’t my Expect script running in cron?

0
0
Asked By CuriousCoder92 On

I have a script that I run every night using cron as the root user. I set the path at the top of the crontab, and the script includes an Expect command to spawn an lftp session. While everything works perfectly when I run the script interactively, it fails to send the file when triggered by cron. The log doesn't show any errors, which is puzzling. Here's the relevant Expect command from my script: `expect -f expect_script.txt`, and the contents of `expect_script.txt` include steps to connect to the FTP server, change directories, and upload HTML files. I suspect the issue might be related to environment variables or path settings that cron doesn't recognize. Any insights on why this might be happening?

4 Answers

Answered By CronConnoisseur42 On

Just a reminder—cron runs in a limited environment. It helps to load your profile or set the working directory within your script. Adding `set +x` for verbose logging can give you more insight into what your script is doing during execution. You can capture this in a log file to check what outputs you’re getting from your commands.

Answered By TechieTom89 On

Make sure you're using absolute paths in your scripts, especially when running through cron. Cron operates in a minimal environment and doesn't know about the paths like your normal shell does. Also, it could help to add logging to your script—try using something like `expect -f expect_script.txt &> /tmp/debug.log` to see if you can catch any errors.

DebuggingDude77 -

That's a solid point! You could also insert an `echo "I am here
" >> /tmp/expectlog.txt` in your script to see if it even gets called.

Answered By Pathfinder88 On

In interactive mode, run `which expect` to find its exact path, then use that in your crontab. For example: `/opt/local/bin/expect -f /full/path/to/expect_script.txt`. Also, don't forget to add logging to see what's happening with your script.

Answered By ScriptSavvy On

There are many potential issues here. First off, ensure that Expect and lftp are in the PATH for the cron environment. You might also want to confirm that `expect_script.txt` is in the correct working directory. Adding error outputs can be helpful, but also consider switching to using `lftp -f` or `-c` to bypass the Expect command altogether. Using a `.netrc` file to store your credentials could simplify your script as well.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.