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

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?

This question is related to c linux gcc cron crontab

The answer is


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.


If you're using Ubuntu, you can put a shell script in one of these folders: /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


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


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


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


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.


Examples related to c

conflicting types for 'outchar' Can't compile C program on a Mac after upgrade to Mojave Program to find largest and second largest number in array Prime numbers between 1 to 100 in C Programming Language In c, in bool, true == 1 and false == 0? How I can print to stderr in C? Visual Studio Code includePath "error: assignment to expression with array type error" when I assign a struct field (C) Compiling an application for use in highly radioactive environments How can you print multiple variables inside a string using printf?

Examples related to linux

grep's at sign caught as whitespace How to prevent Google Colab from disconnecting? "E: Unable to locate package python-pip" on Ubuntu 18.04 How to upgrade Python version to 3.7? Install Qt on Ubuntu Get first line of a shell command's output Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? Run bash command on jenkins pipeline How to uninstall an older PHP version from centOS7 How to update-alternatives to Python 3 without breaking apt?

Examples related to gcc

Can't compile C program on a Mac after upgrade to Mojave Compiling an application for use in highly radioactive environments Make Error 127 when running trying to compile code How to Install gcc 5.3 with yum on CentOS 7.2? How does one set up the Visual Studio Code compiler/debugger to GCC? How do I set up CLion to compile and run? CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found How to printf a 64-bit integer as hex? Differences between arm64 and aarch64 Fatal error: iostream: No such file or directory in compiling C program using GCC

Examples related to cron

How to run a cron job inside a docker container? Run CRON job everyday at specific time How to run a cron job on every Monday, Wednesday and Friday? Spring cron expression for every day 1:01:am How to run a cronjob every X minutes? CronJob not running Scheduling Python Script to run every hour accurately How to set a cron job to run every 3 hours Execute PHP script in cron job How to create a Java cron job

Examples related to crontab

Run CRON job everyday at specific time How to set a cron job to run every 3 hours mysqldump & gzip commands to properly create a compressed file of a MySQL database using crontab How to install crontab on Centos Running a simple shell script as a cronjob Crontab Day of the Week syntax How to run crontab job every week on Sunday Running a cron job at 2:30 AM everyday How to specify in crontab by what user to run script? Using crontab to execute script every minute and another every 24 hours