问题
Hi I am really new in Linux:D
I made a crontab program which is supposed to print current time in console every 3 minutes.
What I did is below.
I made a crontab. In terminal, command "
crontab -e
" and add a phrase "*/3 * * * * /home/user/a.out
"a.out is a result file of "
gcc WowCron.c
".
Code is below.
int main (int argc, char* argv[]){
time_t now;
time(&now);
printf("this is what we call cron does: %s\n", ctime(&now));
return 0;
}
and it works wonderfully when run individually.
Then I ran a "
service cron restart
" command in terminal. Now when I command "crontab -l
", I can see the messages what I wrote in crontab.The problem is somehow I think it works, but never prints time message.
Q. How can I make this print time every 3 minutes?
回答1:
Cron triggers a new process to start in the background. You configure it through a terminal (which is a process) but it has nothing to do with that terminal otherwise. Each process has it's own STDOUT, STDIN, STDERR so as the cron tasks is on a new process it won't print to your terminal process' STDOUT
As tripleee says if you'd like it to print syslog is a good place to go, or you can make it append to a file of your choice.
If you just want the program to run at a time interval in a terminal then a Shell Script is probably a better option:
while :
do
date
sleep 180
done
Or you can replace the "date" function with "./a.out" and run it from the same directory
回答2:
The standard output from a cron
job does not end up on the console. Try using the syslog facility.
Alternatively, if you don't need to integrate this into a larger C program of your own, use the logger command.
*/3 * * * * logger Still here ...
(The system log already includes a time stamp.)
Any standard output and standard error from a cron job ends up being sent by email to the job owner. Maybe you should examine your mailbox, or maybe your email is not working properly?
来源:https://stackoverflow.com/questions/17390221/how-can-crontab-print-messages-in-console