0

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
Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
Vlad
  • 145
  • 3
  • 15

2 Answers2

1

Cron jobs run with a limited set of environment variables.

To see what the environment looks like, you can create a temporary cron job like this:

* * * * * printenv > cron-env

The most likely culprit is the $PATH variable. It's likely that the mpg123 command is in a directory that's in your $PATH in an interactive shell, but is not in the default $PATH provided to cron jobs.

UPDATE : I see that the mpg123 command is provided (on Ubuntu) by the mpg321 package, which presumably installs it as /usr/bin/mpg123. But I don't know what the command does, or how you can tell that it didn't run. Your first step in debugging this should be to try running a simple command so that you can clearly tell whether it ran or not. And just be sure, you are feeding your crontab to the crontab command, yes? If so, it should appear in the output of crontab -l.

Keith Thompson
  • 230,326
  • 38
  • 368
  • 578
  • Turns out you were right, it was the environmental variables, I did more research and I was able to get it all to work but have a script set the variables then launch the program. Here is how I got it to work: export DISPLAY=:0.0 export XAUTHORITY=/home/vlad/.Xauthority #Runs main Alarm check C program. /home/vlad/MIA/Alarm/Alarm_Main – Vlad Apr 03 '17 at 17:40
0

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
Vlad
  • 145
  • 3
  • 15