You are here: command line shell scripting » how to send append output to file in linux command line
How to send / append output to file in linux command line
- Written By
- PHPin24
- Submitted At
- 2009-08-28 07:03:37
- Num Views
- 1641
- Category
- Command line, Shell Scripting
|
When running certain commands in Linux command line you would in most cases not want to see the output on screen but rather have them output / write to a log file. This can be achieved very easily by just using the > command. For example: I have a script called today.php that outputs the current date and time to screen. This can be run by calling it from within your browser, from command line or from a cron <?php echo "This script ran:".date('Y-m-d H:i:s'); If you run it manually via your browser it's not a problem as the browser displays the output, same with command line as it gets printed on screen. However if you are running it through a crontab you will need to be able to check what it has outputted to screen, but you can't as the cronjob runs as the user who installed it without you being able to see the output as it happens in the background. Say you want to run your script every hour of each day 5 minutes past the hour. Ex (10:05 , 11:05, 12:05, 13:05 ...) and you want it to write to a file truncating the file to zero length (clearing the file) and then write to /home/phpin24/log/today.log. Here is what you need to do: 5 * * * * /var/www/html/today.php > /home/phpin24/log/today.log The output in the file will look like this if you have installed the crontab at 08:00 on the 28th of August 2009: 2009-08-28 08:05:00 If however you want to achieve the same, but you want it to append to file like in most cases then just add an additional > to the already existing >, like this: 5 * * * * /var/www/html/today.php >> /home/phpin24/log/today.log If the script has run 5 times and you have installed the crontab at 08:00 on the 28th of August 2009, the output in the file will look like this: 2009-08-28 08:05:00 2009-08-28 09:05:00 2009-08-28 10:05:00 2009-08-28 11:05:00 2009-08-28 12:05:00 By PHPin24 @ 2009-08-28 07:03:37
|
