CronAid v


It will take time ... to check everything it can.

\n"; continue; }else{ echo "Advice: With this time spec, the cornjob is executed at the crowded moment,
it's better to specify the minute field at odd time. ex. 17 * * * * 2){ $f6 = $f[2]; }else{ $f6 = ''; } }else{ if( count($f) < 5){ echo "${pink}No command? Possibly wrong format.$pe"; continue; } for($fi=0;$fi< 5;$fi++){ ob_flush(); if($fi < 3 and preg_match('/([^-0-9*,\/])/',$f[$fi])){ echo "${pink}Wrong character in the time field (".htmlentities($tarr[$fi][1]).")'".htmlentities($f[$fi])."'$pe,\n"; continue; } $el = explode(",", $f[$fi]); $first = 1; $iimax = count($el); for($ii=0;$ii< $iimax;$ii++){ ob_flush(); $suf = $tarr[$fi][2]; $e = trim($el[$ii]); if(substr($e,0,1) == '*'){ if(substr($e,1,1) == '/'){ $u = substr($e,2).' '; $ss = 's'; if($u > $tarr[$fi][0]){ echo "
${pink}Error: the number following '*/' is too big$pe
\n"; $ii = $iimax+100; } }elseif(strlen($e) > 1){ echo "
${pink}Error: You cannot use '".htmlentities(substr($e,1))."'following '*'$pe
\n"; $ii = $iimax+200; }else{ $u = ''; $ss = ''; } echo "every $u".$tarr[$fi][1]."$ss, "; $ii = $iimax+1; }else{ $es = explode( '-',$e); #echo "|$e|"; if(count($es)== 2){ $u = substr($es[1],strpos($es[1],'/')+1)." "; if($u){ $ss2 = 's'; $ss1 = "once "; $ess = substr($es[1],0, strpos($es[1],'/')); }else{ $u = ''; $ss2 = ''; $ss1 = ''; $ess = $es[1]; } echo "${ss1}every $u".$tarr[$fi][1]."$ss2 between ".$es[0]."$suf and ".$ess."$suf "; }elseif(count($es) > 1){ echo "
${pink}Error: Wrong format. Too many '-' used.$pe
\n"; $ii = $iimax+300; }else{ echo "$e, "; } } } if($ii == $iimax){ echo $tarr[$fi][1].", "; } } $f5 = $f[5]; $fcom = join(' ', array_slice($f,5)); if(count($f) > 5){ $f6 = $f[6]; }else{ $f6 = ''; } } $fcom0 = $fcom; $fcom0 = str_replace('\\%', '%', $fcom0); $fcom3 = preg_replace('/([\\\\`"])/', '\\\\\1', $fcom0); $fcom2 = preg_replace('/([\\\\"`$])/', '\\\\\1', $fcom0); $fcom1 = preg_replace('/([><\'&])/', '\'\1\'',$fcom); #echo '
',htmlentities(`HOME=$home;echo $fcom1`),'
'; $f5x = trim($f5,'\''); if($f5 != $f5x){ $mess = "\nNote. Stripped single quotes around the command for testing.\n"; $mess .="Before>>>".htmlentities($f5)."<<< After>>>".htmlentities($f5x)."<<<"; $f5 = $f5x; } if( preg_match('/[`=\'"]/', $f5)){ #$mess = "\nNote. Comand check skipped\n"; $f5 = ''; }else{ $f5x = trim($f5,'('); if($f5 != $f5x){ $mess = "\nNote. Stripped '(' ahead of the command for testing.\n"; $mess .="Before>>>".htmlentities($f5)."<<< After>>>".htmlentities($f5x)."<<<"; $f5 = $f5x; } $f5x = trim($f5,'{'); if($f5 != $f5x){ $mess = "\nNote. Stripped '{' ahead of the command for testing.\n"; $mess .="Before>>>".htmlentities($f5)."<<< After>>>".htmlentities($f5x)."<<<"; $f5 = $f5x; } $f5 = '"'.str_replace('"','\\"',$f5).'"'; #echo '
f5=',htmlentities($f5),'
'; } $fcom = '"'.str_replace('"','\\"',$fcom).'"'; #echo '
',htmlentities($fcom3),'
'; # Messy shell script part ... # But it does better job with less effort than writing it in PHP. $ctmp = "$home/etc/cronaid.tmp"; $acom =<<$ctmp; if sh -n $ctmp ; then echo 'Command syntax check: OK! ' ; else echo '[[Command syntax error! (checked by "sh -n")]]\n' ; fi; rm $ctmp ; ENDMARK; if($f5 != ''){ #echo '
f5=',htmlentities($f5),'
'; $acom .=<<',htmlentities($acom),'
'; if(0 and $f6 and (!strpos($f6, '>'))){ $f6x = trim($f6,'\''); if($f6 and $f6 != $f6x){ $mess = "\nNote. Stripped single quotes around the 1st argument for testing.\n"; $mess .="Before>>>".htmlentities($f6)."<<< After>>>".htmlentities($f6x)."<<<"; $f6 = $f6x; } $f6 = '"'.str_replace('"','\\"',$f6).'"'; echo '
',htmlentities($f6),'
'; if(0 and substr_count($f6,'"')%2){ $ascr = ''; $mess .="\nNote. Skipped arg check because of unbalanced double quote\n"; }else{ $ascr = ";echo -n '1st argument $f6 ' ;if [ -f $f6 ] ;then echo 'exists';else if [ -d $f6 ] ;then echo 'exists';else echo '[[does NOT exists.]] (Normal for options and parameters)' ;fi;fi;"; #echo '
Ascr
',substr_count($f6,'"'),htmlentities($ascr),'
'; } }else{ $ascr=''; } 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:\n
"; echo htmlspecialchars($fcom0)."\n

\n"; $prep = 'for V in `export` ; do unset $V;done;unset V;export PATH="/usr/bin:/bin"; export SHELL=/bin/sh;'; $result = htmlentities(`$prep exec 2>.ERR;cd $home;export HOME=$home;export USER=$user;export LOGNAME=$user;$fcom0 ;echo "\n--- error output (if any) ---\n"; cat .ERR; rm .ERR`); if(!$result){ $result = "\n

Failed to execute the command...

\n";} echo "Result: (If you have redirection such as '>>cron.log', '2>>cron.err', the results will go there.)
".$result.'
'; } echo '

Execute this command

'; } $mess = ''; } echo "
Check done. ### Note: This check detects only several types of errors. ###"; } ?>
MinHrDayMoWeek Command (and redirects)
* means 'Every (min,hr..)'   */5 means 'Every 5 (min...)'




General Tips

    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:$HOME

    Logs

    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>&1

    Crontab 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>&1

    Trouble 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.
    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.