How to update code using svn with crontab? - php

I am using following steps to update code on server after login to server:
sudo -s
<wrote password here>
cd /var/www/staging
rm -r app.old
mv app app.old
svn checkout https://example.com/projectname/trunk/app app
Now I created update.sh file in /var/www/ with following content
cd /var/www/staging
rm -r app.old
mv app app.old
svn checkout https://example.com/projectname/trunk/app app
And I have following crontab entry to run after every 5 minutes:
*/5 * * * * /var/www/update.sh
Problem: So cron job is working but it is only deleting the app folder and not checking out it from svn repository. But when I run bash /var/www/update.sh manually it works fine after sudo -s.
How to fix this for cron job as well. Is it related to sudo -s or something else?
Thanks

I suspect the problem is that the job is running from cron as root and root user doesn't have permissions to checkout.
First, I suggest that you include details of the username/password to use with SVN - inside your script:
svn checkout --username USER --password PASS ttps://example.com/projectname/trunk/app app
Next, change the line in your crontab to this:
*/5 * * * * /var/www/update.sh 2>&1| mail -s "Cron job execution" youremail#wherever.com
Then, when the job executes, any errors would be sent to you via email. At least you'll see what is going wrong.

If you need to run it as a different user, then install it on that users crontab. Rather than trying to sudo over in the script.

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.

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

How to run laravel cronjob in local linux system?

I have a cronjob with name changeflag. I can use following terminal command to execute this cronjob in local.
php artisan changeflag
In hosting server, I can easily set the execution of console command with cron job.
Is it possible to run above command periodically in local system in Linux automatically as in server ?
or
We have to execute above command through terminal for every test ?
I am using LAMP.
Any help is appreciated.
if you want to add your project's cron jobs in crontab, just add them in crontab file:
change editor to nano (optional)
setenv EDITOR nano
open crontab file
crontab -e
add this line
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
replace path-to-your-project with the real path and the cron will be executed automatically.
If this doesn't work, replace php with full php path. To find the whole path just type in command line which php.
For more info read the docs

How to set cron in to run PHP file on localhost in Ubuntu?

I want to execute a PHP script on my localhost in Ubuntu. I have tried a lots of methods to apply cron but no luck!
We can set cron on our localhost through following steps:
Open crontab in your terminal:
EDITOR=gedit crontab -e
Add your cron settings at the end of your file (we are setting cron to be execute in every 2 minute in our example):
*/2 * * * * /usr/bin/php -q /path/to/phpfile.php > /dev/null
Save your cron file and run:
sudo service cron restart

Categories