How can crontab print messages in console?

2019-09-02 13:41发布

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?

2条回答
叛逆
2楼-- · 2019-09-02 14:02

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

查看更多
神经病院院长
3楼-- · 2019-09-02 14:05

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?

查看更多
登录 后发表回答