Cronjob Homestead not working - ssh

I've created a command to send automatic emails. When I do homestead ssh and I run php artisan emails.send an email arrives in my mailtrap.io account.
I've added this code to the kernel.php
$schedule->command('emails:send')->everyFiveMinutes();
I've put it at a 5 minute interval, just to make it easier to quickly test it.
I've ssh'd into Homestead and performed
php /home/vagrant/Code/soeptime/artisan schedule:run 1>> /dev/null 2>&1
then I exit homestead and I did homestead provision
However, there is nothing in the logs and I still haven't received an email, homestead is now running for more then 15 minutes.

You need to manually edit crontab:
First:
crontab -e
and then add
* * * * * php /home/vagrant/Code/soeptime/artisan schedule:run

From the Laravel docs:
Laravel provides a convenient way to schedule Cron jobs by scheduling
a single schedule:run Artisan command to be run every minute. The
schedule:run command will examine the job schedule defined in your
App\Console\Kernel class to determine which jobs should be run.
If you would like the schedule:run command to be run for a Homestead
site, you may set the schedule option to true when defining the site:
sites:
- map: homestead.test
to: /home/vagrant/code/Laravel/public
schedule: true
The Cron job for the site will be defined in the /etc/cron.d folder of the virtual machine.

Related

Cron job permission denied

I'm running a python script called TGubuntu.py.
I used ls -l , and the permissions of the script are -rwxrwxrwx 1 ubuntu ubuntu 503 Jan 13 19:07 TGubuntu.py, which should mean that anyone can execute the file, right?
But I still get in the log /bin/sh: 1: /home/ubuntu/TestTG/TGubuntu.py: Permission denied for some reason.
When I run the script manually it works perfectly.
Any Ideas?
I put it in the sudo crontab like this
* * * * * /home/ubuntu/TestTG/TGubuntu.py
But even in the root (cron) mail log it says Permission Denied!
Couldn't figure out what the problem was, so I accomplished my goal using a different method.
I ran a python script that uses the schedule module to call my script. Then I just let the "Timer" run on screen indefinitely.

restart apache service automatically using cron 12AM daily

I have a CentOs setup in test server.
I wanna to run a cron job (the cron needs to run apache server at 12AM) daily.
My cron.daily fodler is located in /etc/cron.daily
Please let me know the steps how to implement this.
Usually I use to restart the apache service using the below command:
service httpd restart
I wanna to do restart apache service automatically using cron 12AM daily.
Thanks in advance.
While #einterview's answer is almost correct, it's important to note that a * in the minute column will run the job every minute of that hour. If intending to run once every hour, steps would be:
SSH into server.
Get list of current user's jobs with $ crontab -l
Edit jobs list with $ crontab -e (default editor will open)
Add 0 4 * * * service mysql restart for mysql at 4:00am
Add 0 5 * * * service apache2 restart for apache2 at 5:00am
Add 0 0 * * * service apache2 restart for apache2 at 12:00 am
Save and close (Ctrl+O and Ctrl+X in nano)
Recheck with $ crontab -l
I got it and give you step by step adding cron jobs into your system:
Login to your server with SSH
Type crontab -l to display list of cron jobs,
Type crontab -e to edit your crontab,
Add 0 4 * * * /etc/init.d/mysqld restart to restart Mysql everyday at 4 AM,
Add 0 5 * * * /etc/init.d/httpd restart to restart Apache everyday at 5 AM and
Add 0 24 * * * /etc/init.d/httpd restart to restart Apache everyday at 12 AM
Save your file,
Recheck with crontab -l
Get the path for service by running: which service. This should return something like /usr/sbin/service
Add entry to contrab via crontab -e and enter the following:#daily /usr/sbin/service httpd restart
If you do not want an email sent to you whenever it is run, you should instead add the following: #daily /usr/sbin/service httpd restart > /dev/null 2>&1
To find what time cron daily runs, run: grep run-parts /etc/crontab
PS: It is important to get the full path to service.
It wasn't spelled out in the other answers so I'll say it here. There is a different list of cron jobs for the current user and the root user. On my Raspberry Pi 4, doing it the way above does not work because the current user doesnt have permission to restart the service.
This works however:
sudo crontab -l (List current jobs)
sudo crontab -e (Edit cron job list)
0 0 * * * systemctl restart openvpn.service (Add this line to the bottom)
Save and close (Ctrl+O, ENTER, Ctrl+X in nano)
sudo crontab -l (Validate job was added)
In other words, "crontab -l" will give a different list than "sudo crontab -l". Adding "sudo" to the above commands makes the job run as root.
You can use following command:
crontab -e
Add following line to cron:
0 12 * * * service httpd restart
or use following command.
echo "0 12 * * * service httpd restart" | crontab -
This site is a good one for cron time https://crontab.guru
I am not allowed to comment yet on the last one here, but actually you can just use 0 0 * * * then it will go through a-ok.
Tried on ubuntu 20.04.3 LTS
sudo crontab -e
0 8 * * * /home/<user>/restart_service.sh
# Runs above crontab 8AM everyday.
Inside restart_service.sh
#!/bin/bash
systemctl restart my_service.service
Later provide appropriate permissions for execute
chmod u+x /home/<user>/restart_service.sh
following this advice adding:
0 12 * * * /etc/init.d/httpd restart
0 24 * * * /etc/init.d/httpd restart
I get "/tmp/crontab.D6cOzs/crontab":3: bad hour
errors in crontab file, can't install.
i had to do 12 only then it worked, so I'm assuming 24 is unacceptable

Run a php script in background on debian (Apache)

I'm trying to make a push notification work on my debian vps (apace2, mysql).
I use a php script from this tutorial (http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2).
Basically, the script is put in an infintive loop, that check a mysql table for new records every couple of seconds. The tutorial says it should be run as a background process.
// This script should be run as a background process on the server. It checks
// every few seconds for new messages in the database table push_queue and
// sends them to the Apple Push Notification Service.
//
// Usage: php push.php development &
So I have four questions.
How do I start the script from the terminal? What should I type? The script location on the server is:
/var/www/development_folder/scripts/push2/push.php
How can I kill it if I need to (without having to restart apace)?
Since the push notification is essential, I need a way to check if the script is running.
The code (from the tutorial) calls a function is something goes wrong:
function fatalError($message)
{
writeToLog('Exiting with fatal error: ' . $message);
exit;
}
Maybe I can put something in there to restart the script? But It would also be nice to have a cron job or something that check every 5 minute or so if the script is running, and start it if it doens't.
4 - Can I make the script automatically start after a apace or mysql restart? If the server crash or something else happens that need a apace restart?
Thanks a lot in advance
You could run the script with the following command:
nohup php /var/www/development_folder/scripts/push2/push.php > /dev/null &
The nohup means that that the command should not quit (it ignores hangup signal) when you e.g. close your terminal window. If you don't care about this you could just start the process with "php /var/www/development_folder/scripts/push2/push.php &" instead. PS! nohup logs the script output to a file called nohup.out as default, if you do not want this, just add > /dev/null as I've done here. The & at the end means that the proccess will run in the background.
I would only recommend starting the push script like this while you test your code. The script should be run as a daemon at system-startup instead (see 4.) if it's important that it runs all the time.
Just type
ps ax | grep push.php
and you will get the processid (pid). It will look something like this:
4530 pts/3 S 0:00 php /var/www/development_folder/scripts/push2/push.php
The pid is the first number you'll see. You can then run the following command to kill the script:
kill -9 4530
If you run ps ax | grep push.php again the process should now be gone.
I would recommend that you make a cronjob that checks if the php-script is running, and if not, starts it. You could do this with ps ax and grep checks inside your shell script. Something like this should do it:
if ! ps ax | grep -v grep | grep 'push.php' > /dev/null
then
nohup php /var/www/development_folder/scripts/push2/push.php > /dev/null &
else
echo "push-script is already running"
fi
If you want the script to start up after booting up the system you could make a file in /etc/init.d (e.g. /etc.init.d/mypushscript with something like this inside:
php /var/www/development_folder/scripts/push2/push.php
(You should probably have alot more in this file)
You would also need to run the following commands:
chmod +x /etc/init.d/mypushscript
update-rc.d mypushscript defaults
to make the script start at boot-time. I have not tested this so please do more research before making your own init script!

Rails 3 / whenever cron not firing

I am using whenever to fire a rake task every 5 minutes for my app.
schedule.rb:
every 5.minutes do
rake "audit",
:environment => 'development'
end
"whenever" in console:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /Users/john/Sites/rambler && RAILS_ENV=development bundle exec rake audit --silent'
"rake audit" in console works properly.
So all looks good .... except it doesn't work. Nothing happens every five minutes.
Is this because I am trying to run it in development / local?
Thanks!
You need to update your cron file every time you change it.
After you have addded your cron job do this:
whenever --update-crontab 'project_name'
Also I only found whenever working fine in production mode only.
UPDATE:
I have found that we can use whenever in development mode also. Just add
set :environment, "development"
set :output, {:error => "log/error.log", :standard => "log/cron.log"}
to your scehdule.rb file. ( The log one is optional but still you can use that for testing purpose)
Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
see this answer for working in dev mode: Cron not working in Whenever gem
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in ubuntu): sudo service cron restart
check if crontab are good implemented on the server: crontab -l
That is!

rake task in cron

I have a shell script (/home/user/send_report.sh) that runs my rake task:
cd /home/user/rails/app
/home/user/.rvm/gems/ruby-1.9.2-p136/bin/rake report:send
When I run it in console sh /home/user/send_report.sh it works properly.
I am trying to make a cron task with my script: */10 * * * * sh /home/user/send_report.sh, but nothing happens! Rake task should send mail, but this does not happen.
Content of /var/log/cron.log:
Jun 2 21:40:01 ubuntu CRON[1253]: (user) CMD (sh /home/user/send_report.sh)
Jun 2 21:40:01 ubuntu CRON[1251]: (user) MAIL (mailed 240 bytes of output but got status 0x0001#012)
Please, help me to get the working rake script with crontab.
Apart from the fact that you should use /bin/sh, i don't see anything wrong on the cron job. When you run manually, you get the email as you said right ? It does not only work when you do it with cron ? It seems that it could be a misconfiguration of the email server or maybe that the mail server port is blocked ?
Problem was that RVM single-user installation doesn't supports cron tasks. Installed RVM as multi-user and crontab worked properly.