Jump to content

Php script & cron jobs do not works on background


Recommended Posts

Hello everyone, I'm new to this forum, I have the next problem I hope someone can advise.

 

I have a php script I need to run from cronjobs.

 

This script contains some commands shell_exec, fairly simple and the output is directed to a file, from the webmin module cronjobs goes well

 

but when running from cron, the commands shell_exec stops working

 

the cronjob is

 

* * * * * php /some/path/myscript.php

 

to make it easy

 

#!/usr/bin/php -q
<?php
$iptables=shell_exec('iptables -S ');
$file_name="/some/path/out_test.txt";
$ar=fopen("$file_name","w");
fputs($ar,$iptables);
fputs($ar,"nn");
fclose($ar);
?>

 

from the terminal when I run $ php /some/path/myscript.php as root, the file /some/path/out_test.txt is filled with the output of the command $ iptables-S

 

but when run from cron, the file is blank

Edited by denn0n
Link to post
Share on other sites

are you checking in an error log? it sounds like the user the cron is running under doesn't have privileges to run that script. where as from the command line, you are probably running as root.

Link to post
Share on other sites

are you checking in an error log? it sounds like the user the cron is running under doesn't have privileges to run that script. where as from the command line, you are probably running as root.

 

i do apreciate your help thescientist but it is solve , between you have right and other things, the paths to the commands have to be full and for some init process on ubuntu you should put sudo before and the error log it is a great ideai put this afther the comand on cron to get a new file log error 2>/var/log/crontab.err

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...