Crontab on dedicated Ubuntu server - php

I have a php script I want to execute regularly using a crontab. I've done it on my own Apache/Ubuntu 11.04 server by using this code:
*/10 * * * * cd /var/cron/ && /usr/bin/php -q script.php && echo "Update Successful: $(date)" >> update.log
But on my dedicated server (Ubuntu 10.04) I can't get the crontab to execute the script, no matter what I try. I can't find the php binary using "locate php | grep bin" but I've tried referring to both the /etc/apache2/php.ini and the php.ini-production.cli files and a countless other ways recommended online.
I have the file-setup exactly the same on both servers, and have tried executing the php script outside the crontab and it works fine. Also, if I don't try to execute the php script but just uses it to put the text into the log file, the crontab work as well. Would be thankful for some advice! :)

can't you just use
*/10 * * * * /usr/bin/php -q /path/to/file/script.php
I add
> /dev/null 2>&1
to the end to chuck out any output.
and put the logging in script.php

I actually figured it out after many hours of despair. First I had to:
sudo apt-get install php5-cli
sudo updatedb
And finally remove a hash in /etc/php5/cli/conf.d/mcrypt.ini which was causing a failure cause of depreciation. Now it works! Thanks for you time though Dagon!

Related

Crontab not executing commands in docker

I'm building a web app where instructors provide courses and customers buy them.
I needed automation for changing courses statuses. Pretty much just checking some conditions and e.g. closing course when the current time is greater than deadline.
I'm using php-symfony, everything is dockerized. So to access my php container, I have to start docker-compose and type docker-compose exec php bash. Well in php I have a command which I can run manually, to check all the courses statuses and possibly do some changes to database if the conditions are met. But to automate this process, I decided to use crontabs. I've tested the command in cmd whether I have all the access to do such thing and it worked, so I just put it into crontab to execute every minute.
* * * * * cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1
The first parts gets to the project folder, the second part opens docker container and runs the command that checks courses statuses. In cmd the whole command worked. And even in /var/log/syslog which is supposed to be default crontab logging file, it gave me this output:
May 8 16:42:01 martin-ubuntu CRON[1032921]: (martin) CMD (cd /home/martin/PhpstormProjects/bp_project && sudo docker-compose exec php php bin/console courses-check >/dev/null 2>&1)
I don't see any visible errors but looking in the database, it didn't change any values while it should.
So I was thinking it could be something with docker?
I tried to add second command in crontab which is:
* * * * * cd /home && echo "hi" > a.txt
And it was saving "hi" into given file every minute.
So while the first command doesn't work, this one does. Any ideas where is the hidden problem?
change your crontab into this
* * * * * sudo docker-compose -f /home/martin/PhpstormProjects/bp_project/docker-compose.yml run --rm php sh -c ' bin/console courses-check' >/dev/null 2>&1

Linux scheduled task not working in Laravel

The below cronjob is not working, although the task itself is working when I manually run it using php artisan q:calc .
I just added the path for php and artisan files as shown below, and pasted the command in the terminal.
Am I missing something ?
* * * * * /usr/bin/php /var/www/html/sharp/artisan schedule:run >> /dev/null 2>&1
That command is a cron entry, not something you run in terminal.
For example, under the specific user you would run (depending on your environment):
$ crontab -e
And paste the above to the crontab file.
You can learn more in the docs:
https://laravel.com/docs/master/scheduling
Or by researching how to add cron entries for your specific operating system.

Crontab isn't running Laravel scheduled jobs

I've set up crontab on my AWS-EC2 instance to hit the Laravel scheduling endpoint every minute via the root account using sudo crontab-e:
* * * * * php ~/htdocs/artisan schedule:run >> /dev/null 2>&1
However, despite to the cron logs showing it is indeed running every minute:
Jan 26 12:02:01 ip-172-31-28-116 CRON[5057]: (root) CMD (php ~/htdocs/artisan schedule:run >> /dev/null 2>&1)
the job itself isn't executing.
Running the command php ~/htdocs/artisan schedule:run >> /dev/null 2>&1 straight up triggers the job and works.
I'm really struggling with what is going wrong here, am I missing something?
So, I failed to heed the cron output "No MTA installed, discarding output" - Upon installing an MTA (postfix, via sudo apt-get install postfix), it turned out that for the cronjob, php wasn't findable.
Changing the command to use the output of which php to:
/opt/bitnami/php/bin/php /home/bitnami/htdocs/artisan schedule:run
is now working.
Thanks for your help!
Use absolute paths when adding cron entries. ~/htdocs/artisan that should be set using the full path to your application root directory.
It works when you manually run the command because your environment is set accordingly. Not the case when adding cron entries using sudo.

Running codeigniter 3 cron job issue

I'm trying to run a codeigniter 3 cron job. If I open the file manually it works through the browser and I find databse updated and emails are sent
https://www.example.com/module_name/controller/method
But not working through a cron job like this every minute on a private server
curl --silent https://www.example.com/module_name/controller/method
Also tried
/usr/local/bin/php /home/username/public_html/index.php module_name controller method
Any idea or other ways to run it?
I'd first go to a terminal and check that your php is actually at /usr/local/bin/php by running:
which php
You mentioned that you'd like to know other ways to run the cron, and I've used wget many times. For you that would look something like:
/usr/bin/wget https://www.example.com/module_name/controller/method -O /dev/null
In most(all?) Linux distros, you're going to open crontab for editing using:
crontab -e
Once in there, just add a line:
* * * * * /usr/bin/wget https://www.example.com/module_name/controller/method -O /dev/null
Do ensure that you have wget available, and it's location by running:
which wget

Cannot execute PHP script periodically using crontab on Ubuntu

I want to execute a php script every 5 minutes. I'm using Ubuntu and I followed these steps:
Executed crontab -e from terminal, entered:
*/5 * * * * /usr/bin/php /var/www/test1.php
in the nano text editor, saved it and started the crontab. It gave no errors and said "installing new crontab", but my script is not being executed. I gave the necessary permissions to the files I use in my script, too.
Any help would be greatly appreciated, thanks.
This is what I use for scheduling cron jobs on apache server in cpanel interface :
/usr/bin/php -q /home/domain_name/public_html/cron_test.php
So, you should specify the path to the php executable too, for making the php script work and execute.

Categories