How to set up a cron job to run an executable every hour?

CLinuxGccCronCrontab

C Problem Overview


I need to set up a cron job that runs an executable compiled using gcc once every hour.

I logged in as root and typed crontab -e

Then I entered the following and saved the file.

0 * * * *  /path_to_executable

However, the cron job does not work.

I see that when I type /...path_to_executable I get a segmentation fault. I can only execute the executable from the folder it is located in. Is there a way I can solve this problem?

C Solutions


Solution 1 - C

0 * * * * cd folder_containing_exe && ./exe_name

should work unless there is something else that needs to be setup for the program to run.

Solution 2 - C

The solution to solve this is to find out why you're getting the segmentation fault, and fix that.

Solution 3 - C

You can also use @hourly instant of 0 * * * *

Solution 4 - C

If you're using Ubuntu, you can put a shell script in one of these directories: /etc/cron.daily, /etc/cron.hourly, /etc/cron.monthly or /etc/cron.weekly.

For more detail, check out this post: https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

Solution 5 - C

Did you mean the executable fails to run , if invoked from any other directory? This is rather a bug on the executable. One potential reason could be the executable requires some shared libraires from the installed folder. You may check environment variable LD_LIBRARY_PATH

Solution 6 - C

use

path_to_exe >> log_file

to see the output of your command also errors can be redirected with

path_to_exe &> log_file

also you can use

crontab -l

to check if your edits were saved.

Solution 7 - C

Since I could not run the C executable that way, I wrote a simple shell script that does the following

cd /..path_to_shell_script
./c_executable_name

In the cron jobs list, I call the shell script.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnonymousView Question on Stackoverflow
Solution 1 - CjoastView Answer on Stackoverflow
Solution 2 - CDavid ThornleyView Answer on Stackoverflow
Solution 3 - CMarek SkibaView Answer on Stackoverflow
Solution 4 - CJames GentesView Answer on Stackoverflow
Solution 5 - CJayanView Answer on Stackoverflow
Solution 6 - CjartiedaView Answer on Stackoverflow
Solution 7 - CAnonymousView Answer on Stackoverflow