# # cronaid.php # # It will let you add, edit, and check the crontab. # It will detect common errors like line ending, missing newline at the end, # missing etc directory, wrong permission, and so on. # # How to use? # Put this script in a well protected directory and just call it. # http://yourdomain.com/protected/cronaid.php # # NO Editing required. # # Please DO NOT edit this file unless you know what you are doing. # # # #### WARNING: ##### # # As this script can EDIT the crontab, # it is VERY IMPORTANT to put it in a WELL PROTECTED directory. # At least, password protect the directory and/or this script. # It's better if you limit access by IP range, as well as the password. # # Keep a backup copy of crontab handy, just in case. # # It is created for people who can't handle crontab with normal means, # and it doesn't offer very precise detailed examination of your crontab. # Nevertheless, it will detect and automatically correct some of common errors. # # This script is designed for PowWeb setup. # It will not work in other environment, and usually not needed. # # # v0.50 Initial version # v0.51 Fixed 'unwanted slashes bug' # v0.60 Modified command checking routine # v0.61 Added comment skipping # v0.70 Added time field checking and command syntax check, more checks.... # v0.80 Added experimental "try the command" feature. Turned off by default. # v0.81 Added/corrected some examples # v0.82 Fixed cwd when checking command syntax. Removed a few bugs # v0.83 Added ob_flush() here and there to output results as it goes # v0.84 Fixed shell syntax check bug # v0.85 cron execution simulation updated # v0.86 correction for typo, and additional tips # v0.87 Log copy setup link added # v0.88 Correction in the session deleting example and bug fix # v0.89 Shell syntax check improvement. Ecabled test feature by default. # $ver='0.89'; $user = get_current_user(); $home = "/www/".substr($user,0,1)."/$user"; $etcperm = 0700; # permission for $HOME/etc directory $ctabperm = 0600; # permission for the crontab file $etc = "$home/etc"; $ctab = "$etc/crontab"; $EXECUTE = 1; # cron execution simulation $min = rand(6,54); $uid = posix_getuid()-500; $udir = substr($uid, 0,1)."/$uid"; ?>
\n"; echo `ls -al "$etc"`; echo "
',preg_replace('/\[\[(.+?)\]\]/','\1', htmlentities(`exec 2>&1 $acom $ascr`));
echo $mess,'';
# "Try the command" feature.
if($EXECUTE){
if($_GET['mode'] == 'exe' and $_GET['l'] ==$i){
echo "Executing the command:\nFailed to execute the command...
\n";} echo "Result: (If you have redirection such as '>>cron.log', '2>>cron.err', the results will go there.)".$result.'
PowWeb specific details
While the crontab format used on PowWeb servers are standard, the way you enter crontab IS non-standard. Instead of using builtin vi type editor to edit your crontab, you can edit/upload the crontab in anyway you want, and then, it will be picked up. There is a delay in the pickup. This is an important factor. Also, since it's a normal text file you are editing/uploading, all kinds of errors can slip in. And these errors will cause the new crontab to be ignored. By using this tool, you can avoid most common errors associated with editing/uploading. Also, this tool perform command syntax check, and other checks to detect obvious mistakes.Examples
Daily job (write uptime to the log on 04:34 AM PST/PDT) 34 04 * * * uptime >>etc/cronlog.txt 2>&1 Daily job (run /www/U/USER/htdocs/hidden/daily.php on 05:17 AM PST/PDT) 17 05 * * * /usr/local/bin/php htdocs/hiedden/daily.php >>etc/cronlog.txt 2>&1 Daily job (run /www/U/USER/etc/daily.sh on 05:17 AM PST/PDT) 17 05 * * * ./etc/daily.sh >>etc/cronlog.txt 2>&1 Daily job (copy both access and error log to your own directory. You must create /www/U/USER/logs) It's one long line. Add "logcopy (user specific version)" to my crontab! 42 3 * * * (D=`date -v-1d "+\%Y-\%m-\%d"`;L=`id -u`;L=$(($L -500));LD=${L\%${L#?}}/${L};cp -v /logs/access.0/${LD}_access_log logs/${D}_accec_log;cp -v /logs/error.0/${LD}_error_log logs/${D}_accec_log;) >>logs/easycopy.log 2>&1 Similar to above. (access_log only.) 45 2 * * * L=`id -u`;L=$(($L -500));cp -v /logs/access.0/${L\%${L#?}}/${L}_access_log $HOME/logs/`date -v-1d "+\%Y-\%m-\%d"`_access.log >>$HOME/logs/easycopy.log 2>&1 Deleting session files (older than 4hours) every 4 hours at 24th minutes. (Modify session file path depnding on your setup.) 24 */4 * * * * find $HOME/sessions/ -mtime +4h -delete >>etc/cronlog.txt 2>&1 Another example of Deleting session files. (Session life of two to four hours.) Dlete files with 'sess_' prefix (older than 2hours) every 2 hours at 42th minutes 42 */2 * * * * find $HOME/sessions/ -name 'sess_*' -mtime +2h -delete >>etc/cronlog.txt 2>&1 Copy & merge php.ini of PowWeb and yours (in etc/myini.txt) to produce custom php.ini 27 03 * * * cat /usr/local/lib/php.ini etc/myini.txt >htdocs/php.ini 2>>etc/cronerr.txt Copy php.ini of PowWeb (eliminating comments) and merge your settings (in etc/myini.txt). 22 02 * * * grep -h -v -e '^ *;' -e '^ *$' /usr/local/lib/php.ini etc/myini.txt >htdocs/php.ini 2>>etc/cronerr.txt You can create/edit 'myini.txt in the etc directory with WebFTP of PowWeb. It may contain such item as session_save_path. session.save_path = '/www/U/USER/sessions' For a simple task like deleting session files, creating custom php.ini, you don't need heavy&slow PHP. It's better to run your cronjob at "odd time" to avoid "cron traffic jams".Current directory
When cronjob starts, the 'current directory' is /www/U/USER. So, You DON'T need to write '/www/U/USER' part if you prefer so. Ex. 38 */4 * * * /usr/local/bin/php htdocs/some.php >>logs/cronlog.txt 2>&1 --- htdocs/some.php is equal to /www/U/USER/htdocs/some.php. --- Also, Env variable $HOME is set to the same directory, if you wish to use.PATH
Command search path in cronjob is less than CGI environment. Other than commands in /bin and probably /usr/bin, use full path, or set the path by yourself. PATH=$HOME/mytool:$HOMELogs
Even you don't feel you need it, or you don't know well, take the logs, just in case. If you take logs, don't forget to remove them periodically. (Otherwise, they can grow ....) If you don't mind combined log of both standard output and errors, you can do like this. Ex. 38 */4 * * * /usr/local/bin/php htdocs/some.php >>logs/cronlog.txt 2>&1Crontab pickup delay
IMPORTANT: As I've mentioned already, your crontab isn't picked up instantly. It takes minutes and longer, sometime. DO NOT set a crontab that execute every minutes for testing, and leave it there. It may get picked up later, and you may have lots of mails or some problems. In general, it's better to place a shell script in the crontab, and edit it instead of crontab itself. This way, any change is instantly effective. But you need to know a few basic things about shell script and using UNIX.For PHP people
To run a php script via cron, you must call the script through php command. BAD: 38 */4 * * * htdocs/some.php >>logs/cronlog.txt 2>&1 Good: 38 */4 * * * /usr/local/bin/php htdocs/some.php >>logs/cronlog.txt 2>&1 If you put the shebang line on the top of your script; #!/usr/local/bin/php and IF you set executable permission to the script, the 'BAD' example would work, though. But most PHP users have problem doing these ... ALSO, EVEN with the php command, many php scripts will not work from cron. You can try phpwrap.cgi in these cases. Then, you can give parameters to the PHP script. 17 * * * * etc/phpwrap.cgi 'http://yorudomain.com/subdir/some.php?abc=123&def=345' >>etc/cronlog.txt 2>&1 PHP5 The path to php5 binary is: /usr/local/php5/bin/php As of 2005/07/14, PowWeb recompiled php5 binary so that it works in cronjob without exporting REDIRECT_STATUS env variable. 42 03 * * * /usr/local/php5/bin/php htdocs/some.php >>etc/cronlog.txt 2>&1Trouble shooting
- First, run this script. :) And pay attention to the outputs. - 2nd, make sure the command you want to run exists, and make sure the command has no error. (Syntax, permission, and so on) - 3rd, check the permission of the command 500,700,(or 755). NOT 777, 666, 644. php scripts can be anything readable if you use /usr/local/bin/php to run it. - 4th, line ending issue and missing new line should be checked and corrected with this script. - 5th, check time field specification, again. - 6th, remember the "Pickup delay". It takes time. Sometime it doesn't get picked up at all. But often, it doesn't get picked up because of some errors. FAQ of PowWeb says it get picked up 'every minute'. But I'm not at all convinced. - 7th, when you are not sure if the command line is picked up and executed, put date command (with the logging redirect): From: */5 * * * * yourcomaand >>log/log.txt 2>&1 To: */5 * * * * date >>logs/log.txt; yourcomaand >>log/log.txt 2>&1 You will see the output from the date command if the line is executed. - 8th, as the cronjobs are executed on the syslog servers, sometime you may encounter a problem you don't see on the web servers. You may have to test and find out the difference in the server setups. It's PowWeb's responsibility to setup servers correctly. But it may speed up the fix if you can pinpoint the problem and tell them, rather than sit and hope and wait, and wait, and wait. - 9th, If you are using PHP script or any other script in your crontab, make sure that script has no error. To check if the crontab is picked up, you can replace the script with very simple one. ie. echo 'OK'; ?>If you want to know more, please read Tutorials, FAQ, Forum, Forum search, and other helper sites. See also, Crontab format Shell command format But I think this program shows almost everything average user needs to know. :) NOTE: This tool is provided by a user. NOT by PowWeb. Everything written and shown by this script is based on my personal knowledge, and it has nothing to do with PowWeb's opinion. Although I tested it to work correctly, it may still contain bugs and mistakes. If you don't like it, correct it or write another one by yourself.