I'm trying to set up a cronjob script on my server. I've followed this tutorial and I now have a folder "scripts" with "cronjob.php":
<?php
define("_CRONJOB_",true);
require(APPLICATION_PATH . '../public/index.php');
// my executions
?>
In my "index.php" file:
if(!defined('_CRONJOB_') || _CRONJOB_ == false)
{
$application->bootstrap()->run();
}
But how can I set this on my server?
I've done the following as a start: chmod 755 cronjob.php, but what's next?
Use crontab, make sure you have both crontab and php-cli installed.
First edit the cron by doing
$ crontab -e
Then insert something like this
*/10 * * * * /usr/bin/php /path/to/scripts/cronjob.php
This examples does execute the script every 10th minute.
For more on the syntax see https://en.wikipedia.org/wiki/Cron#Predefined_scheduling_definitions
Alternate method for setting Cron job on Linux Server
Step 1: Open Terminal
Step 2: Type
$ sudo crontab -e
Enter System Password
Step 3: Put Cron URL to be executed for every 5 minutes interval
*/5 * * * * curl http://testwebsite.com/hitcronscript
Reference: https://crontab.guru/every-5-minutes
*Note: For test purpose one may set mail or file/Db entry.
Hope it will help developers.
Related
I'm trying to run my first cron job on MAC OS (my localhost is running MAMP).
To access crontab, to run a cron job, I'm typing the following into the command line:
crontab -e
Then I press i on the keyboard to go into insert mode to insert the cron job.
The cron job inserted is
* * * * * wget http://localhost:8888/project/cron.php
Then I press esc and type :wq in the command line.
I then get a message:
installing new crontab
Then I check what cronjobs are running with crontab -l and it says:
* * * * * wget http://localhost:8888/project/cron.php
But nothing happens...
On a separate note, below is the script I'm executing as a test script in the cron.php file. It is meant to generate a new time stamp in a text file in the root of the project (the cron job is supposed to make this happen every minute):
<?php
$file = dirname(__FILE__) . '/output.txt';
$data = "hello, it's " . date('d/m/Y H:i:s') . "\n";
file_put_contents($file, $data, FILE_APPEND);
?>
If anyone has any advice as to why this isn't working I'd be hugely grateful.
I've tried the steps shown above at both system level (i.e macmini) and also in my 'project' folder.
I'm completely new to all of this so any help would be amazing.
The solution to this was to use the file paths not the url. First I found where the php was located. I did this with:
whereis php
This gave me the following file path:
/usr/bin/php
I then got the FULL file path to the cron.php file in my project:
/Applications/MAMP/htdocs/project/cron.php
I then combined the two in the crontab cron job:
* * * * * /usr/bin/php /Applications/MAMP/htdocs/project/cron.php
The actual steps I used are the same as in my original question.
I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh
I have some crontab set on server on linux platform.Before that 2 days all the cron was running.I dont know what happen with crontab that they are not working now.
All the cron was running before and i have added a new crontab after that they are not running may be this is the problem or is there other problem with that.
I have check ther permission but that is ok with.
New cron i have add look like that:
*/15 * * * * php myproject/sendmail.php
30 5 * * * php myproject/sendmailOnDiscount.php
* */1 * * * php myproject/sendInvitaion.php
The last one have added and before that other was running well.
After adding crontab sendInvitation.php crontab has stop working.
Could any one tell me why crontab is not working now.(All the crontab has stop working)
Maybe it is off, you can turn it on with this command
service crond start
Mostly this problem occurs due to script file permission and ownership of script files. The same problem was faced by me. I found that my script owner was not a super user e.g. root.
So, you have to set the permission and ownership of your scrip as super user. Find below.
First of all edit your crontab as super user.(in RHEL like below)
[abc#host] crontab -e
and save crontab :wq!
Now set permission for script
[abc#host] chmod +x script.sh
[abc#host] chown root:root script.sh
Now restart your crontab.(in RHEL like below)
[abc#host] /etc/init.d/crond restart
I have a cronjob that runs the following:
* * * * * php /path/to/phpfile.php >> /cronlog.txt
when I run the php file in bash everything works, but when the cronjob runs it, one command fails:
shell_exec("redis-cli ping"); and returns an error that sh: 1: redis-cli: not found
Does anyone know why the cron user using PHP shell_exec would not be able to use the redis-cli command?
update
git diff /env_term.txt /env_cron.txt
-SHELL=/bin/bash
-TERM=screen
-SSH_CLIENT=*************
-SSH_TTY=/dev/pts/0
-USER=root
-LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.
-TERMCAP= { a bunch of giberish }
-PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
-MAIL=/var/mail/root
-STY=*************
-PWD=*************
-LANG=en_US.UTF-8
-HOME=/root
-SHLVL=2
LANGUAGE=en_US:en
+HOME=/root
LOGNAME=root
-WINDOW=2
-SSH_CONNECTION=*************
-LESSOPEN=| /usr/bin/lesspipe %s
-LESSCLOSE=/usr/bin/lesspipe %s %s
-_=/usr/bin/env
+PATH=/usr/bin:/bin
+LANG=en_US.UTF-8
+SHELL=/bin/sh
+PWD=*************
Did you check if your PATH variable is the same when cron is called.
A quick check is to add a dummy cron job to output the current environment variables passed to cron:
* * * * * env > /tmp/env.out
And then compare this output with when you run the env command from the terminal
try this solution:
cat cronjob
* * * * * php /path/to/phpfile.php >> /path/to/cronlog.txt
Then:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
You can give absolute path of redis-cli to avoid any such issue related to environment variable.
I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.