How can crontab print messages in console?

拥有回忆 提交于 2019-12-25 01:45:37

问题


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.

  1. I made a crontab. In terminal, command "crontab -e" and add a phrase "*/3 * * * * /home/user/a.out"

  2. 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.

  1. 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.

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!