1

I need some help as my script doesn't create a file. I want to run this script at cronjob. I am using Debian Linux with PHP and Apache.

My script is locate at /var/www/myapplication folder. I want to create / write a file at /var/www/myapplication/testfolder.

The commands I use in the php script is:

echo `whoami`;
exec ('whatever >> testfolder/testing.txt');

Folder rights:

drwxrwxrwx 2 www-data www-data 4096 Jul 27 09:55 testfolder
  1. When I run the script directly from browser (http://127.0.0.1/myscript.php)

    • it doesn't create the file (testing.txt)
    • echo whoami says: www-data
  2. When I run the script from the server with root rights (su):

php -q /var/www/myapplication/myscript.php >>log3.txt

  • it creates the file (testing.txt)
  • echo whoami says: root
  • log3.txt is created at /var/www/myapplication folder

    1. When I run the script from crontab:

              • /usr/bin/php -q /var/www/myapplication/myscript.php >>log3.txt
  • it doesn't create the file (testing.txt)

  • echo whoami says: root
  • log3.txt is created at /root folder

Why isn't testing.txt created my I run my script from crontab as a cronjob ?

4
  • Why aren't you using the builtin PHP file functions? Commented Jul 27, 2015 at 8:18
  • EM-Creations: Because I want redirect 'whatever' command output to create the necessary file (testing.txt). Commented Jul 27, 2015 at 8:21
  • 1
    Try shell_exec. Also, make sure the www-data user has correct write permissions for the directory. Commented Jul 27, 2015 at 8:29
  • EM-Creations: Thank you. I tried shell_exec, but the result is same: testing file is not created, only the log3.txt. log3.txt contains: root Commented Jul 27, 2015 at 8:38

1 Answer 1

1

You shouldn't be executing operating system commands directly, there are already functions and objects avaliable in PHP for writing and read files. As well, you shouldn't be giving them permission to be writing files to /. Even though you're running it from localhost, if you ever expose the service you're allowing public users to run operating system commands.

You also mention in your comment that you want to redirect the output of 'whatever' to a file, but I still don't see why you couldn't do this with the PHP provided functions.

Regardless, to answer the original question when you run the script via localhost you're accessing it via Apache which means the user www-data is running the PHP script. When you access it via your cronjob you're calling PHP from the commandline and another user is accessing the service.

You can see the error that is being returned from attempting to write to the file by logging in with the user running the cronjob and using echo exec(...).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.