crontab如何在控制台中打印消息? [英] How can crontab print messages in console?
问题描述
我真的是Linux新手:D
Hi I am really new in Linux:D
我制作了一个crontab程序,该程序应该每3分钟在控制台中显示当前时间。
I made a crontab program which is supposed to print current time in console every 3 minutes.
我的工作如下。
-
我做了一个crontab。在终端中,命令
crontab -e
并添加短语* / 3 * * * * / home / user / a .out
a.out是结果文件 gcc WowCron.c
。
a.out is a result file of "gcc WowCron.c
".
代码如下。
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.
-
然后我在终端中运行了
service cron restart
命令。
现在,当我命令crontab -l </ code>时,我可以看到我在crontab中写的消息。
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。如何每3分钟打印一次?
Q. How can I make this print time every 3 minutes?
推荐答案
cron的标准输出
作业不会在控制台上结束。尝试使用 syslog
工具。
The standard output from a cron
job does not end up on the console. Try using the syslog
facility.
或者,如果不需要将其集成到自己的大型C程序中,请使用 logger
命令。
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.)
cron作业的任何标准输出和标准错误最终都会通过电子邮件发送到老板。也许您应该检查您的邮箱,或者您的电子邮件无法正常工作?
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?
这篇关于crontab如何在控制台中打印消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!