3

Say I'm running a C program via crontab -e:

15 11 * * * time /home/philip/school/a1_c_program.c > /home/philip/logs/time2execute.txt

But the C program requires the user to enter some text.

What happens in this situation? Does the C script just remain an open process on my machine until I restart? Or does it automatically close after some time?

If I could attach to the process and enter the required text would the time command finish running and print something to the newly created text file?

I noticed with my above cronjob the time2execute.txt file is created but is empty.

Edit: Once I fixed my crontab to point to the compiled c program instead of the source file. The text file included the prompt text that the user would seen.

11

Tools running by the cron daemon get an environment from the cron, and not from your shell. The cron doesn't provide a standard input for these tools, more exactly it provides a faked one from the /dev/null device.

Thus, if the tool requires standard input, it will get an end-of-file error as it tries to read in the input data. It depends on the tool, what will do it with that. In most cases it will be the same as if you would run it by calling the program with a

programname </dev/null

If it tries to read the terminal directly, like ncurses apps, then its terminal initialization sequence won't work. The ncurses_init() call will give it an error result. It depends on the tool also in this case, what it does with the error result. Most tools simply exit with an error message. It depends on the cron configuration, what will it do with this error message (by default, it logs and sends you in an email).

Side note: Your cron line is bad, you try to run a .c source code directly. First you have to compile it to a binary executable. Furthermore, the time tool puts its output to the standard error output, it would be nice for you to redirect its output (and also its std error) to some log file.

  • 1
    oops thanks, I had compiled the programs but accidentally linked to the wrong one in crontab – Philip Kirkbride Feb 18 '17 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.