Let's say I want to run this simple script every 5 min and get the output sent to my email ?
On a usual server I'd simply do:
$ cat echo.sh
#!/bin/sh
echo "Hello"
$ crontab -l
*/5 * * * * /whatever/bin/echo.sh
This would send me an email with the script name as the subject and Hello as a body every 5 minutes.
How do I get the same behavior on Heroku ?
Here is a quick solution that send you an email every 10 min. The heroku scheduler doesn't provide shorter intervals. Install this two add-ons:
https://addons.heroku.com/sendgrid (Send the email)
heroku addons:add sendgrid
https://addons.heroku.com/scheduler (Schedule the job)
heroku addons:add scheduler
Then you add the flowing script mail.sh to your app:
#!/bin/sh
SGTO=receiver#example.com
SGTONAME='Some Name'
SGSUBJECT='Email Subject'
SGFROM=from#example.com
SGTEXT='Email Text'
curl -d "to=${SGTO}&toname=${SGTONAME}&subject=${SGSUBJECT}&text=${SGTEXT}&from=${SGFROM}&api_user=${SENDGRID_USERNAME}&api_key=${SENDGRID_PASSWORD}" https://api.sendgrid.com/api/mail.send.json
Finally go to your scheduler and add a new job. Specify bash mail.sh as the task.
Related
By default crontab jobs stdout is sent to the email of the crontab user like the crontab file says in the description:
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
I want to use slacktee same as using (tee) to post a message to slack inside the script run by crontab.
In other scripts (daily scheduled by putting them in /etc/cron.daily/ directory) I do this:
echo "New message!" | slacktee -i "tada" -c "messages"
And when they run, there are no issues. But in my custom scheduled script I see no message on slack though they run correctly.
My job starts every four hours with this scheduling:
* */4 * * * /scripts/mysql_backup.sh
I've tried many redirecting (inside the script, inside the cron command) but anything seems working.
How can I use slacktee inside my custom sheduled script?
EDIT:
I'm a bit late sorry for that and thanks for all your time.
#PhilDenfer no, slacktee does not log anything on /tmp.
#Gedge i've tried sudo echo "test" | slacktee.sh and it works. Also using su and then doing echo "test" | slacktee.sh works.
#isp-zax cron jobs runs successfully because the script makes the backup of the database.
Redirecting the stderr I got slacktee.sh command not found. So using slacktee.hs as root works but not when root uses it in crontab scheduled job (in a daily scheduled script i use slacktee successfully). Why?
Redirecting the stderr I got slacktee.sh command not found. So using slacktee.hs as root works but not when root uses it in crontab scheduled job (in a daily scheduled script i use slacktee successfully). Why?
Because the PATH variable for user root and for user cron are different.
Instead of just 'slacktee' use a full path, i.e. /usr/local/bin/slacktee and it should work OK.
I understand I can set a cron job to run every 5 minutes with crontab -e by adding a line such as: */5 * * * * /path/to/script.sh.
Is it possible to get the system time in minutes using date +"%M" for example, and then set a cron job to run at date +"%M" plus 5 minutes?
I know I can get date +"%M" + 5 via the following process:
$ MIN=`date +"%M"`
$ export MIN
$ expr $MIN + 5
Is it possible to use this to set a cron job or script to run at "current time in minutes" plus "X minutes"?
I could imagine this being useful in an application in which a user creates a new document and then is prompted to save or title the document X minutes after creating it.
You should use the at command instead.
With at, you can specify the time when a command should be run using time or even keywords like midnight, teatime, tomorrow etc..
You can specify the time after 5 min like this:
at now + 5 min
And then enter the command you want to schedule. Or you can enter your scheduled jobs in a jobs file and give it as a argument for the at command using the -f option.
Sample of a jobs file:
$ cat myjobs.txt
/path/to/a/shell-script.sh
/path/to/any/command/or/script.sh
The following command will execute those jobs after 5 mins:
$ at -f myjobs.txt now + 5 min
Check this link for more information.
Look at the at command. Maybe this is what you are looking for.
See
http://www.computerhope.com/unix/uat.htm
http://www.thegeekstuff.com/2010/06/at-atq-atrm-batch-command-examples
Yes you can:
*/5 * * * * /path/to/script.sh && crontab -r
I need to perform a cronjob which sends emails when a perticulor condition fails. Cronjob should run everyday. And How to test the cronjob in local machine (I am using ubuntu 12.04).
I have already setup the sendmail for php. mails are going from magento.
Thanks in advance.
First, to run PHP from a command line on ubuntu:
sudo apt-get install php5-cli
The way to handle exceptions in PHP is
try {
} catch (Exception $e) {
mail('test#example.com', 'Error subject of thing failed', "Body of thing failed\n".print_r($e,1));
}
If you want the script to stop once the error has occurred, just put a die() or exit after the mail() call.
But your question is a little light on details since we don't know what's failing or where. It could just be that your PHP code is checking for X in the OS/filesystem, then all you need is to call mail() when the condition is found.
As far as the cronjob side goes you need to do this:
crontab -l > crontab.txt
Use editor of choice to open crontab.txt
Add a line at the bottom of crontab.txt (example below)
crontab crontab.txt
Then run crontab -l | less to review the new crontab to make sure it is correct
This will run every day at 1:45am server time
45 1 * * * /usr/bin/php /path/to/script/to/run.php
http://en.wikipedia.org/wiki/Cron#Examples
You can test the script by running it manually or setting the cronjob to start in the near future instead of 1:45am
I've been working on a project and I would so not like it to be taken by system crash.
So I wrote a script to backup my whole project directory into Dropbox.
But I had to run it every 10 min, if I could remember to do that.
Question: any way to auto-it-up?
Type the following command to enter cronjob:
$ crontab -e
To get crontab to run a task every 10 minutes you could type as follow:
*/10 * * * * /path_to_script
See additional read for it:
Wikipedia
cron-every-5-minutes
cron job every 5 minutes starting from a specific time
try this solution:
cat cronjob
*/10 * * * * sh /path/to/scipt.sh
Then:
chmod +x /path/to/scipt.sh
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
The obvious solution is by crontab, as others suggest. Here is another solution, by adding only one line to your backup-script.
If your system has installed and enabled the "at" suite, see:
man atd
man at
or atrun if you using bsd like system
you can simply queue the "next run-time" from your backup script. So, for example, if your backup script is called /home/joe/bin/copy_to_dropbox, add to the end of script the next line:
af -f /home/joe/bin/copy_to_dropbox now +10 minute
and run manually the backup script first time.
After the first backup, the at command in the script queues itself for the next execution.
You can check the at queue with the "atq" command.
I'm pretty new to using MySql from the command line, so I really need some advice here.
Basically, I've written a bash script that backs up my databases on selected days via a cron job. It's working just fine, but I would like to know if there is any way I can direct any error messages from mysqldump emailed to me in the off chance that there's something wrong. Here's the key part of the code that's doing the dump:
mysqldump -u user -h localhost --all-databases | gzip -9 > $filename
Is there any way to set up a condition that would capture any error messages and send them in an email?
Blain
Use:
mysqldump -u user -h localhost --all-databases 2> error.log | gzip -9 > $filename
In particular, in bash you can redirect any output descriptor to something else by using the n> syntax, notice the LACK of space between n and > :)
Email the error.log to yourself :)
You said you're using cron to run the job. Which is great, because cron already has a mail sending feature built in--no need for writing to a temporary file nor using support scripts. Just do this:
MAILTO=yourname#example.com
00 14 * * * mysqldump ... | gzip -9 >filename # or invoke a script here
And like magic, any output from the job will be mailed to you.