Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between running an executable straight from the command line and from a cron job in Linux?

I have this executable that queries a remote server for a command, executes it on the local machine and returns the stdout (and also possibly stderr) from it back to the server.

This executable runs just fine if called from the command line (as root), but I found it's failing for some commands when executed automatically by the cron job.

What are the differences in terms of environment (users, stdin, stdout, etc.) I should expect when scheduling this executable to run periodically using crontab?

Thanks!

like image 376
kolrie Avatar asked Dec 22 '25 14:12

kolrie


1 Answers

The most important difference is that files like .bashrc etc. are not executed before cron jobs, so a lot of environment variables that you normaly have in the command line will be missing. So if your program doesn't work in the cron job, embedd it in a script that sets all the necessary environment variables.

Regarding input and output, there is obviously no user interaction for cron jobs, so the programs should not expect input (if they do, provide it from an input file or directly in the script), and any output should be redirected into a log file.

like image 170
Erich Kitzmueller Avatar answered Dec 24 '25 03:12

Erich Kitzmueller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!