Search code examples
csystemcron

system() not working in C on cron job


The system() function does not work when the program is run from a cron job, but it works fine when I manually execute the program.

C Code:

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

char command[256];

strcpy(command, "mpg123 /home/vlad/MIA/Alarm/test.mp3");
printf("Commnd Executed");
system(command);

return 0;
}

Crontab File:

*/1 * * * * /home/vlad/MIA/Alarm/test >> /home/vlad/MIA/debug_test.txt

Solution

  • It seems it was a combination, the cron job was not running, I was able to figure that out with the help of Keith Thompson, as well as cron job needed some evnormental variales. The work around I did was to lanuch the C program from a script with the variables needed.

    .sh file

     #/bin/bash 
    
    ##Needed to have cron play an audio file
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    export DISPLAY=:0.0
    export XAUTHORITY=/home/vlad/.Xauthority 
    
    #Runs main Alarm check C program. 
    /home/vlad/MIA/Alarm/Alarm_Main