1

I've wrote a PHP-Script, and the execution requires sudo permission, because it executes some other system related stuff. So i've added this into the crontab -e but i can't see, that my script is running correctly.

Command:

*/5 * * * * /usr/bin/php /srv/www/php/script.php && date > /srv/www/php/mylog.log

The only output is the date. No response from my script. It is executable (chmod a+x) and if i use the same command in the terminal as root, everything is okay. So it's up to crontab -e?

Also i made sure, that my cronjob is running (/var/log/syslog):

Oct 23 16:40:01 $MYMACHINE CRON[13797]: (root) CMD (/usr/bin/php /srv/www/php/script.php && date > /srv/www/php/mylog.log)

And this every 5 mins. Also my "mylog.log" always got the latest timestamp, so i guess, there's might be a problem with the script inside.

EDIT: I've created another test script to check, if the scripts getting executed or not. But the script was executed, so it's a problem inside the script.

I am trying to add dynamically IPs to an iptables chain:

#!/bin/bash
value=`cat whitelist.txt`
#echo "$value"

for i in $(echo $value | tr "," "\n")
do
  # process
  /sbin/iptables -I teamspeakCommunication --src $i -j ACCEPT
done

May you can see something weird?

4
  • @AD7six please see my edit :)
    – Tyralcori
    Oct 23, 2015 at 14:41
  • Well, I was writing an answer based on the format of your cron line being wrong, but you've since edited your question to be what it should look like. Did you have the initial format in your crontab or the one you currently have in the question?
    – GregL
    Oct 23, 2015 at 15:01
  • Yeah, excuse me. I have the edited on inside my Crontab -e. Also the php error log is empty. But I really doubt, that the problem is not php combined with sudo. I've added the command as root
    – Tyralcori
    Oct 23, 2015 at 15:20
  • Do you expect the output from your script in mylog.log?
    – Niklas S.
    Oct 23, 2015 at 15:58

1 Answer 1

1

Add it to your /etc/crontab file like this:

*/5 * * * * root /usr/bin/php /srv/www/script.php && date > /srv/www/php/mylog.log

If your script's first line is #!/usr/bin/php and execution permission as well, you can call it directly like any other script, like the ones written in bash, perl, etc...

Best regards!

2
  • Pretty nice suggestion! But i transferd my complete logic into a bash script now, and added this into the /etc/crontab - as your line is.But the shell script won't getting executed as well?
    – Tyralcori
    Oct 23, 2015 at 17:10
  • Does it have execution permission? Also, why don't you try to put the redirection into your bash script as well? What does /var/log/syslog say now? It should work, but since it is not, I suggest you debug it sending the output of each and every command to a text-file... Oct 23, 2015 at 17:43

Not the answer you're looking for? Browse other questions tagged .