Specifying path to run php file in cron job - cron

I have written a cron job in my Server as below
35 * * * * /home/sites/domain.com/public_html/admin/filename.php
I am trying to call a file in http://domain.com/admin/filename.php.
In Cron job i navigated to every folder by the way its stored in directory in server.But the way to reach the same file in browser is as below
http://domain.com/admin/filename.php
Now the cron filename.php is not running.I checked the file permission every thing is perfect.
I want to know is there a problem in the path i specified
Thanks in advance

When you run php file in cron. You must config as:
35 * * * * /usr/bin/php /home/sites/domain.com/public_html/admin/filename.php
and you want run daemon in server. You can setup as:
nohup php /home/sites/domain.com/public_html/admin/filename.php &

Related

PHP script via cron is not finding files called in the code

I want to run php script that is located on server at:
/var/www/my_directory/myfile.php
Script executes one function that uses certificate files located at:
/var/www/my_directory/my_cert_dir/cert_file.pem
inside PHP script they look like this:
$key="my_cert_dir/cert_file.pem";
I have also tried to add $_SERVER["DOCUMENT_ROOT"]:
$key=$_SERVER["DOCUMENT_ROOT"]."/my_cert_dir/cert_file.pem";
In both cases I get "file not found" for my certificate files.
However, when run through CLI or browser they execute without problems.
First I used cron like this:
13 19 * * * /usr/bin/php /var/www/my_directory/myfile.php >> /var/www/my_directory/my_logs_dir/some_log.txt
And cron executed the script and I got errors, so I tried to change cron to execute from directory like this:
13 19 * * * cd /var/www/my_directory; /usr/bin/php myfile.php >> /var/www/my_directory/my_logs_dir/some_log.txt
However this time I don't see that script executes and my log is not created.
Any ideas how to fix my issue so that script is executed and cert files are found?
Why not try the full path:
$key="/var/www/my_directory/my_cert_dir/cert_file.pem"

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...

Bash script runs manually in terminal but is not executed from crontab

I have a bash script that I want to be executed every 15 minutes, so I added this line to my crontab:
7,22,37,52 * * * * /path/to/my/script.sh
I've checked the directory path to be correct and the script runs correctly if I just run /path/to/my/script.sh manually from any directory. I have this bang line in my script:
#!/usr/bin/env bash
My script also references other scripts in the same directory as it, and I have run chmod +x on all scripts that are needed. I set the MAILTO to my email address and I was getting some Cron Daemon emails when I changed the line in my crontab to:
7,22,37,52 * * * * sh /path/to/my/script.sh
But I never received emails upon using
7,22,37,52 * * * * /path/to/my/script.sh
or
7,22,37,52 * * * * bash /path/to/my/script.sh
I made sure cron is running and I've also tried redirecting the output of my script to a log file, which is also only written in when I include the sh. However, if I run sh /path/to/my/script.sh from the home directory, it does not work. The only ways my script actually runs is if (from any directory) I call /path/to/my/script.sh or bash /path/to/my/script.sh. I'm pretty new to writing bash scripts so any help is very welcome.
#pvas The cron user environment should be treated with extra special care. The assumption that most users have is that they will have access to paths, directories, permissions etc. This is far from the case. Cron runs in a minimal environment and you must set up EVERYTHING - Paths, Permissions and the location where the scripts are running from.
1) I set up the environment myself.
2) I use fully expanded paths in my crontabs.
3) I make sure any directories that need to be read have read permissions.
4) I make sure that my password does not expire because that will block cron when it does.
5) Make sure underlying scripts are explicitly invoked (by Perl, Bash, Python whatever).
6) Pipe the command on the cron line to a LOG file (even better a log file with a TIMESTAMP).
Fix these things and then try again. Cron is particular, you need to set up everything.
For example:
#SETUP ENVIRONMENT
SHELL=/bin/bash
source /home/userfoo/.bash_profile
#RUN THE SCRIPT everyday at 11:50pm (23:50)
50 23 * * * userfoo /home/userfoo/script.sh >> LOGFILE.txt
<<
Crontab entries should have the following format
m h dom mon dow command
which confirms that your entry below
7,22,37,52 * * * * /path/to/my/script.sh
is correct. Having said that, you must close the crontab editor(:wq) for the changes to come to effect.
It is suggested you go through [ this ] cross site post which portrays the possible issues with cron jobs.
More about hashbang [ here ].

Run script daily with cron and create compressed log file

Im trying to run PHP script on this location on every 15 minutes
0,5,10,15 * * * * /usr/local/bin/php /var/www/html/adform.php
and to put results of the script in specific folder in specific file and to compress it daily. Is it possible and how? Pls help.
This is possible.
Instead of running /usr/local/bin/php every 15 minutes, why not replace that call with a shell script which does everything you described?
This shell script would run your php script, pipe the output to a file somewhere on your system which you can then compress.

Cron job that saves file

Is it possible to run a cron job on host that also saves a file to your website directory? I have an API I download a xml file from hourly. I would like to schedule cron to automate this but I'm not sure if it's possible.
You can write a cron job to execute an arbitrary script. The script can do whatever you like provided it has the appropriate permissions.
* * * * * /bin/execute/this/script.sh
Will run the script script.sh
You need to configure the crontab entry appropriately (obviously).
Yes. You can schedule a script in cron.
In the script you can add lines to generate the file and to place the file in the location you want.
Note: Be sure that you have permissions to create a file in that specific location path.

Resources