Technology Insights

Echo Cron To Syslog

When creating cron tasks on Linux many people do not realize that the output from the cron will logged and without redirection will sent to the user running the cron task’s email. In many cases this is the root user or if an alias has been set in /etc/aliases then the email will be sent to the user configured to receive roots email in the aliases file. Specifically on Backtrack Linux the root users email is redirected to bt5 by default and that user does not exist so the email will simply bounce. Below we describe a better way to redirect the cron tasks output into a log file which could be useful down the road.

Technology Insights

PHP Script Using Exec Function & Launched From Crontab Hogging Server Memory

While working on a PHP script on a CentOS Linux server that runs from crontab I ran into an issue where PHP was bogging the server down because of memory use. The script has a lot of output that is not necessary to store though when using the exec function within the PHP script it was storing the unneeded output to an away which I believe was causing the memory usage problem. At first I thought I simply needed to modify the memory_limit setting within php.ini however that was not the true issue at hand. Below I describe two items to look at modifying if you run into a similar issue.

Code Snippets

Bash Script That Deletes Directories Older Than X Days If Y Number Of Directories Exist

Earlier tonight I created a little script that will run in cron on a Linux server. The script counts the number of directories and files in a specific directory and if the count is above Y then it deletes directories and files older than X number of days. In the example script below the number of items (directories and files) that have to be located in the directory before the script to delete files older than a specific date is 10. If there are ten items then the script will delete items older than 90 days. Below the script is the entry made in a specific users cron on the Linux server.

Technology Errors

Cron Bash Script Error: /usr/bin/env: ruby: No such file or directory

I needed to create a bash shell script tonight that called another shell script with options that then would load a Ruby environment and execute certain commands within a Ruby project. I ran into numerous issues but most were silly things such as typos or other minor issues caused by myself. The one issue I had a little trouble figuring out because of the file that was causing the error related to cron not being able to run “/usr/bin/env: ruby”. My bash script worked perfectly from the Linux CLI shell however when attempting to run it I would always get the same error. Below I list details about the error, where I finally located what the error was, and how to resolve the error.

Technology Insights

How to Modify the timestamp (Date and Time) of a File on Linux

There are certain circumstances where you may want to modify the date and time of a file on Linux. In my case I was testing a cronjob that moved and deleted files older than a certain date and testing it was much easier after modifying the date of the files.

The command to use for modifying the date and time of a file is called touch. To modify the timestamp of a file issue one of the below commands. The file used in each of the examples below is named testfile.