Cronjob not working - trying to run PHP script - php

I am running a cronjob that runs a PHP script to do a couple of things; first, it opens the database and stores the contents of the tables into memcache; second it creates a Javascript file that is basically a couple of arrays so that the client browser can do a lot of the work and save the server from being overloaded. This is newly added.
The script runs manually very well, and for over a year it has done its job, updating memcache every 10 minutes. The Javascript file add on is the big problem here; the cronjob s/b creating a new, updated edition of this file every 10 minutes, but appears to be not working unless I run it manually from the command line.
I can check this by doing:
ls -al id_index.js
and checking the timestamp in the file listing.
Is there some problem with creating a Javascript file from a script started by crontab?
Btw, the cronjob file entry looks like this:
# m h dom mon dow command
*/10 * * * * php /home/accountname/public_html/mc_store_arrays.php
Any and all help is appreciated.

In which directory are you expecting the javascript file to be created? It probably is being created somewhere ... wherever cron's working directory happens to be when the script runs (/root/ perhaps?). Make sure your output file is specified with an absolute path or with e.g:
file_put_contents(__DIR__ . '/id_index.js', $content);
which will create the file based on the path of the php script that's running, rather than the path from which it was executed.

Most likely you will have to specify the absolute path to the php cli interpreter, since the cron environment rarely defines a usable PATH environment variable.

Try using LYNX (just the way you use a web browser);
Example: LYNX http://www.MyDomain.Com/MyScript.php?MyParameter=MyValue&And=SoOn

Related

Ubuntu - Cron job not completing in PHP script that contains include file

I'm inserting a record into my DB each time the cron job runs my script and that works. When the PHP script reaches an include file, the functionality in the file doesn't execute. When I run the script in the browser it all works fine. Is it a cron permission issue to access the include file? I don't see an error in my Ubuntu cron log.
It turns out paths to includes in the PHP file need to be absolute. Adding "/var/www/html/filename.php" worked for me.

Cronjob to detect last modified files

I want to run a cronjob every minute to detect all files that were changed in the last minute in a specific directory (with about 300.000 inodes) and export this file list to a csv.
Is it possible to run an optimized command to do that? I cant run a "find" with sort flag in this directory cause it is huge and it will probably take more than 1 minute to run all files.
Is there any command I can do that? Or run any specific program on the background of the server that logs every changed file as it is changed? If there is a command using PHP to do this I am fine, I can create a cron to execute a PHP script, no problem.
There is a Linux utility called incron that can be used similar to normal cron, but rather than events being time based, they work off of inotify and are fired from file events.
You can find the Ubuntu man page here: http://manpages.ubuntu.com/manpages/intrepid/man5/incrontab.5.html
I personally have not had to use it for anything too complex, but it roughly goes like this:
Install it:
sudo apt-get install incron
Open the editor to add an entry:
incrontab -e
Put something like this:
/var/www/myfolder IN_MODIFY curl https://www.example.com/api/file-updated/$#
The first part is the file or folder to watch. The second part is the event. And the third part is the command.
I think that $# is the placeholder for the file in question.

Running php script with cron: Could not open logfile, so such file or directory

I have a Raspberry Pi set up as a seedbox. I have a cron job that will run every 10 minutes, check for finished files using transmission-remote -l, grep for entries that are done (100%), get the names of the folders, copy these to the external drive, and then delete the original files on my Pi.
For every action that is done (A torrent is added, a torrent is finished, a file transfer has started, a file transfer has finished and the files have been deleted) an entry is written to my logfile.log, which is located in the same directory as all scripts, ´/home/pi/dev/´. Inside that folder I have a subfolder, logs that keeps logfiles on all moves from the pi to the external drive. These logfiles are all named after the folder/file being moved.
As I said, every 10 minutes, torrentfinished.phpis run through the cron job
*/10 * * * * php -f /home/pi/dev/torrentfinished.php
All output from the job is sent to my mail at /var/mail/pi.
Now, if I run the script manually, I can run it from anywhere by writing
php -f /home/pi/dev/torrentfinished.php
I have some debug lines written in right beneath the execution of each command. (I use shell_exec to run the commands because I'm more comfortable writing in php than bash).
It'll output
Started transfer
Wrote transfer to logfile
In logfilean entry is then added, with the text $timestamp : started transfer of data from torrent $torrentname. A separate file is created in logs/$torrentname.log. Basically, everything works perfectly.
However, when the cron job runs, I get the following output in /var/mail/pi
Unable to open logfile: No such file or directory
Started transfer
Wrote transfer to logfile
But as you've probably guessed, nothing happens. The files remain in their spot on the Pi and are not transferred. In addition, nothing is written to logfile or logs/$torrentname.log.
I've been wracking my brain over this, and been using chmod 777 on more files than could possibly be considered necessary nor safe, simply to make sure this isn't a permissions issue. I may have missed something of course, but I wouldn't think so. I've also tried renaming the file logfile to something else, but I still get the same error.
I have no more ideas on what to do, so if any of you have ideas, please do tell!
When you use this:
php -f /home/pi/dev/torrentfinished.php
You stay at /home/pi/dev/ directory. And logfile is written at /home/pi/dev/logs
When you run script in cron, base directory is another (for example it may be /bin or /usr/bin).
Try to use DIR or FILE constants to set a logfile path.
It might help to see the actual php code. My first step here would be to have the code print the path to logfile so I could figure out why it thinks it doesn't exist (are you using a relative path or some environment variable, because cron tends to run in a sanitized environment).

What location is a Cron called from?

I have created a simple PHP script that is executed by a CRON daemon on my remote webserver. Every few minutes, the script looks up some JSON data on another website, and stores the results in a file in the same folder as itself.
However, it seems that the file that is used when navigating to the page in the browser, a different file is used than when the cron is executed.
In other words: When going here:
www.example.com/cronscripts/my_script.php the file /home/user_xxx/domains/example.com/public_html/cronscripts/datafile.json is used.
However, when calling the script using the following cron statement:
/usr/local/bin/php /home/user_xxx/domains/example.com/public_html/cronscripts/my_script.php
a file on a different location is used. Is this file in the /usr/local/bin/php folder in the Linux server? If so, I can't access it by hand because I can only access the /home/user_xxx/ folder.
Could someone tell me where the file ends up when using Cronjobs?
This seems to be a path mismatch for the location, you are most probably using a relative path somewhere for the json file.
I think you should schedule your cronjob as following and see if that works
* * * * * cd /home/user_xxx/domains/example.com/public_html/cronscripts && /usr/local/bin/php my_script.php
You should use the absolute path to your script on the server i.e. /var/www/script.php
Try to use instead
*/1 * * * * php /home/user_xxx/domains/example.com/public_html/cronscripts/my_script.php
Using chdir() to ensure you are starting in the proper directory may also help.
Just a side note: please always use #!/bin/env php
(see http://seancoates.com/blogs/use-env for more info)

PHP Script as Cron doesn't work, but does from CLI

I have a php script that triggers some magento actions, and I set it to a cron of:
cd /home/dir/public_html; php -f file.php;
This starts the script, however it does not finish executing for some reason, the cron runs as the user "user", and when I run the command from the terminal as root it works perfectly. All the files it uses are chowned to user however. I thought it was an issue with paths which is why I added the CD command to the front of it, however that wasn't it.
I'm thinking this may be an issue with the creation of a lock file, I have it create a lock file, run the script, then delete the lock file in order to prevent it from running if it already is. The lock file is generated but never deleted, my knowledge is if it creates it as user "user" then it should be able to delete it as that user as well.
Any thoughts? Much appreciated.
Try to put the full path of PHP, or define the PATH variable in the first lines of crontab:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/games:/sbin:/usr/sbin
Edit : moreover, you can log your script like that :
* * * * * cd /home/dir/public_html; /usr/bin/php -f file.php; &>/tmp/file.log
Instead of calling the php from within the cronjob, call a shell script which is calling the php file then.
You can then change the environment the script is running in w/o the need to change the cronjob and you can easier test-drive the cron command (as you can just call the shell-script).
You can then, in the shell script, change the directory where the php-script expects to be in which will most certainly solve your issue.
Additionally you can change ini directives, handle logging and shell error handling, like piping STDERR to a file etc.
This is not exactly your problem, but the information given in this question might solve your issue: How can I force PHP Version for Command Line?.

Categories