This my crontab file:
# Edit this file to introduce tasks to be run by cron.
MAILTO=richardheyes#gmail.com
0 0 * * * ./backup.sh
0 8,12,16,20 * * * /bin/bash /kunden/homepages/46/d548322256/htdocs/dev/admin/sitemaps/create.sh
0 8,12,16,20 * * * `which php7.1` -q /kunden/homepages/46/d548322256/htdocs/dev/admin/sitemap-html/create.php
As you can see there's a backup shell script, a shell script creates a plain text sitemap file and a PHP command-line script that creates a HTML sitemap.
The resulting sitemaps are viewable here:
https://www.rgraph.net/sitemap.txt
https://www.rgraph.net/sitemap.html
The resulting emails from the cron tasks come through to me as intended but with a subject line like this:
Cron <u78819167#infongp-uk31> `which php7.1` -q /kunden/homepages/46/d548322256/htdocs/dev/admin/sitemap-html/create.php
Cron <u78819167#infongp-uk31> ./backup.sh
Which is just plain ugly. Now I could prevent the email by ensuring the tasks don't produce any output or redirecting the output to /dev/null - but if any errors crop up I'd like to see it.
So my question is:
How can I change these butt ugly subjects to something that looks nicer, on a per cron job basis? Is there an environment variable that I can set from each script that would be used as the subject?
Related
I want to disable email reports on some tasks which run frequently. I've gone through the following links
https://unix.stackexchange.com/questions/84335/stop-cron-sending-mail-for-backup-script
https://www.cyberciti.biz/faq/disable-the-mail-alert-by-crontab-command/
They suggest adding >/dev/null 2>&1 at the end of the command to disable emails.
This is my crontab entry :
* * * * * /bin/bash /home/ubuntu/startup/monitor-mosquitto.sh >/dev/null 2>&1
But I'm still receiving emails every time the script is run. In fact, not just once but I get like 8-15 mails every time it runs.
Am I doing anything wrong here ? BTW, I'm using crontab as root ( sudo crontab -e )
See man 5 crontab:
If MAILTO is defined but empty (MAILTO=""), no mail will be sent.
If memory serves, I have used that on the line itself, or preceding it:
MAILTO=""
* * * * * /bin/bash /home/ubuntu/startup/monitor-mosquitto.sh
Note that this will affect all lines that follow it so you may want to place it last, or renable MAILTO.
Also, strictly speaking, you should be able to work out what you did with shell redirection in the shell itself. What you have looks correct so I am a little puzzled. Maybe make sure to test it as root not as you.
I have a bash script that I want to be executed every 15 minutes, so I added this line to my crontab:
7,22,37,52 * * * * /path/to/my/script.sh
I've checked the directory path to be correct and the script runs correctly if I just run /path/to/my/script.sh manually from any directory. I have this bang line in my script:
#!/usr/bin/env bash
My script also references other scripts in the same directory as it, and I have run chmod +x on all scripts that are needed. I set the MAILTO to my email address and I was getting some Cron Daemon emails when I changed the line in my crontab to:
7,22,37,52 * * * * sh /path/to/my/script.sh
But I never received emails upon using
7,22,37,52 * * * * /path/to/my/script.sh
or
7,22,37,52 * * * * bash /path/to/my/script.sh
I made sure cron is running and I've also tried redirecting the output of my script to a log file, which is also only written in when I include the sh. However, if I run sh /path/to/my/script.sh from the home directory, it does not work. The only ways my script actually runs is if (from any directory) I call /path/to/my/script.sh or bash /path/to/my/script.sh. I'm pretty new to writing bash scripts so any help is very welcome.
#pvas The cron user environment should be treated with extra special care. The assumption that most users have is that they will have access to paths, directories, permissions etc. This is far from the case. Cron runs in a minimal environment and you must set up EVERYTHING - Paths, Permissions and the location where the scripts are running from.
1) I set up the environment myself.
2) I use fully expanded paths in my crontabs.
3) I make sure any directories that need to be read have read permissions.
4) I make sure that my password does not expire because that will block cron when it does.
5) Make sure underlying scripts are explicitly invoked (by Perl, Bash, Python whatever).
6) Pipe the command on the cron line to a LOG file (even better a log file with a TIMESTAMP).
Fix these things and then try again. Cron is particular, you need to set up everything.
For example:
#SETUP ENVIRONMENT
SHELL=/bin/bash
source /home/userfoo/.bash_profile
#RUN THE SCRIPT everyday at 11:50pm (23:50)
50 23 * * * userfoo /home/userfoo/script.sh >> LOGFILE.txt
<<
Crontab entries should have the following format
m h dom mon dow command
which confirms that your entry below
7,22,37,52 * * * * /path/to/my/script.sh
is correct. Having said that, you must close the crontab editor(:wq) for the changes to come to effect.
It is suggested you go through [ this ] cross site post which portrays the possible issues with cron jobs.
More about hashbang [ here ].
I am really new to Linux and I apologize if this is rudimentary, but I have Google'd to find examples with no clarity and I am confused. (the question relates to a server running CentOs 6)
My questions are:
I am not sure what is the default directory that I should store a .sh file in so that a cron job can run it.
Is the syntax and sequence of my code in .sh file below correct?.
I have tested the TSQL and its fine.
#! SQL="DELETE FROM messages WHERE date < DATE_SUB(CURDATE(), INTERVAL 7 DAY)"
MYSQL_USER="root"
MYSQL_PASS="xxxxxx"
MYSQL_DB="mydb"
I understand that the cron should contain this to do it on a daily basis:
0 0 * * *
But I am just having some apprehension of how to put it all together so I don't screw things up. A full example or explanation or a reference link would be greatly appreciated.
I believe that cron will execute the script from whichever directory it is in, given that:
the file has execution permission for the user that cron runs as (usually root if job is configured in the system-wide crontab)
the cron line specifies the full path to the script
So, if your script is /opt/script.sh, specifying this in cron:
0 0 * * * /opt/script.sh
will execute script.sh each day in 12:00am.
Please note that if this is the system-wide crontab (/etc/crontab) it should also include a username as which to execute the command:
0 0 * * * username /opt/script.sh
Also, something to make sure when working with cron is to either use full paths when calling external commands from the script or to set up the PATH variable (either in the script itself or on the crontab file). This is needed because usually the environment in which cron jobs are run is pretty restricted.
Another thing to have in mind is that if any output is generated by a cron job this output is sent via mail to the user executing the cron. So to have some feedback from the script you have to either set up the system so that the mail message ends up in a mailbox which is read by a human being or the script sends all of it's output to a log file or syslog.
dear friends i have a jcl program that uses PCO program .I want to schedule it every minute using crontab .i have done the entry in cronntab like below
* * * * * path/job
but the crontab is not producing any output but when i am seeing /var/log/crontab it is showing that crond has executed the job every minute but i am not getting any output
Plz help how to schedule that job
I have read in internet that it can be because of environment variables not set properly
plz tell if that can be the case
Where do you expect the output to show up ? crontab
If standard output and standard error
are not redirected by commands
executed from the crontab entry, any
generated output or errors shall be
mailed, via an implementation-defined
method, to the user.
So you should redirect the output
* * * * * path/job > /path/to/file
Alternatively, check your mail (mail).
I have a perl script which Im planning to run every minute.
I have set the cron job as
* * * * * PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl
I assume the script is executing every minute only because I see a entry like below when I do cat cron in /var/log/
Jul 26 04:57:01 dmvbu-build crond[773]: (root) CMD (PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl)
Jul 26 04:58:01 dmvbu-build crond[687]: (root) CMD (PATH= /usr/local/bin:/usr/bin:/usr/sbin:/usr/lib; perl /dm2/www/html/isos/pre5.3/autoDownload.pl)
But my problem is I have statements like
print LOG "connecting to website\n"
where LOG is a file descriptor to a file named log.txt which is located at
dm2/www/html/isos/pre5.3/ (same place as autoDownload.pl)
But I dont see this log.txt file updating with new informations after I see the entry in the cron log file
But I see this file updating when I run the code manually
You have to remove the extra space after the = in PATH.
PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/lib
The cron line should be
* * * * * PATH=/usr/local/bin:/usr/bin:/usr/sbin:/usr/lib perl /dm2/www/html/isos/pre5.3/autoDownload.pl
Note the lack of a space after the = and the lack of a semicolon before perl.
Provide the absolute Path to the logfile (/dm2/www/html/isos/pre5.3/log.txt instead of just log.txt) when open()-ing, otherwise you have to ensure that the "current working directory" of cron is where you want it to be.
Also check that the user under which this command is executed has write-permissions to the file.
Its much easier to debug a program when you have the output/errors.
You should also use an absolute path to perl in either the top of you script or on the cron line. You should then be able to get rid of any $PATH messyness.
# Make sure you script is executable
chmod a+x /dm2/www/html/isos/pre5.3/autoDownload.pl
Try adding a MAILTO line to cron above your process.
MAILTO="me#example.com"
* * * * * /usr/bin/perl /dm2/www/html/isos/pre5.3/autoDownload.pl
or alternatively logging the cron output to a file to watch for errors. Get rid of /usr/bin/perl in the crontab by making sure its the 1st line of the script #!/usr/bin/perl.
* * * * * /dm2/www/html/isos/pre5.3/autoDownload.pl &> /tmp/autoDownload.log