php - Result form crontab is not same as manually execute? -


i have php script execute shell commands.

once manually executed it. worked fine.

but after used crontab execute, result shell commands missing.

these 2 commands.

  • ip route
  • iptables -t nat -l | grep 8800

and here sample php code.

#!/usr/bin/php -q <?     $route = exec('ip route');     $iptable = exec('iptables -t nat -l | grep 8800');     echo $route;     echo $iptables; ?> 

the above code worked manually execute not crontab.

i found commands worked both. example

  • ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'
  • cat /tmp/example | grep test

any ideas problem, , thank in advance.

a couple of things worth noting:

the exec function's signature

string exec ( string $command [, array &$output [, int &$return_var ]] ) 

to full output of command you'll have call so:

$output = array();//declare in advance best because: $lastline = exec('ip route', $status, $output);//signature shows reference expected print_r($output);//full output, array, each index === new line echo php_eol, 'the command finished ', $status === 0 ? 'well' : 'with errors: '.$status; 

you're using exec command though passthru, might worth in case:

the last line result of command. if need execute command , have data command passed directly without interference, use the passthru() function.

it's important not that, when run command manually, has access environment variables, loaded when log in (and additional vars set in .profile or .basrc file). not case when running command using crontab or on ssh.
perhaps, $path environment variable set so, don't have access ip , other commands. there easy fixes this:

exech('/sbin/ip route');//if that's correct path 

or, have second script @ ready, , change php script to:

if (!exec('which ip')) {//ip command not found     return exec('helper.sh '.implode(' ', $argv));//helper script } 

with helper script looking this:

#/bin/bash source /home/user/.bashrc $* 

where $* merely calls original php script again, time, profile has been loaded. replace source call export path=$path:/sbin or something, , set environment variables way need them.

the third, , final part uses pipes, , proc_open. it's php-only way of loading profile , calling script recursively:

if(!$_server['foo']) {//where foo environment variable loaded in .profile/.bashrc file     $d = array(array('pipe','r'),array('pipe','w'));     $p = proc_open('ksh',$d,$pipes);     if(!is_resource($p) || end($argv) === 'calledfromself')     {//if last argument calledfromself, we've been here before, , didn't work         die('ffs');//this prevent deadlock due infinite recursive self-calling     }     fwrite($pipes[0],'source /home/user/.profile'."\n");     fwrite($pipes[0],implode(' ',$argv).' calledfromself'."\n");     fclose($pipes[0]);     usleep(15);//as long need     echo stream_get_contents($pipes[1]);     fclose($pipes[1]);     proc_close($p);     exit(); } echo $_server['foo'].php_eol;//actual script goes here, can use foo env var 

this how solved an old question of mine encountered difficulties environment variables not being set

last thing note is: user running crontab have permissions required needs done?


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -