dtc88867 2014-09-09 20:48
浏览 20

在cron作业期间从php文件记录输出

I am running a cron job on my local server running Ubuntu 13.10 and want to create a log of the jobs performed. I can access the main log file, syslog, that shows if the cron job ran or had errors.

Here is my cronjob (runs every minute while I am testing, script is finished in under a few seconds)

* * * * * /usr/bin/php -f /home/[usr]/web/X/app/application.php &>> /home/[usr]/Maildir/geocode.log 2>&1 # JOB_ID_2

Inside my application.php file, I have several echos that output variables and results of queries performed:

echo "STARTING CRON @ ".date("m-d-Y H:i:s")."
";
echo count($properties) . " properties from query"."
";

If I run this file from the command line, the file executes correctly and the output is displayed. After looking up other questions about cron logs, I thought I needed to output the log file by the second half of the cron job:

 >> /home/[usr]/Maildir/geocode.log 2>&1

Is this incorrect? How can I save and log the output from my php file? My file selects a number of object from a database and performs actions on them. I want to output the number initially worked on, number successfully completed, and any errors. Right now nothing is outputted, but I know the cron is running because the database it connects to is being updated correctly.

EDIT: I researched some more and thought 2>&1 would pass stderr into stdout because i wanted all of that info, but looks like this was preventing anything from being written. Using:

/usr/bin/php -f /home/[usr]/web/X/app/application.php >> /home/[usr]/Maildir/geocode.log 

Adds all of the output I was expecting into the log file, but I am unsure what will happen with errors because it is running fine now. I will leave this open in case anyone else wants to add anything about creating a log file and if 2>&1 should ever be used in a situation where we want to keep both stderr and stdout.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求帮我调试一下freefem代码
    • ¥15 matlab代码解决,怎么运行
    • ¥15 R语言Rstudio突然无法启动
    • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
    • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
    • ¥15 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?