setting up a cron job every 15 minutes between 8am to 8pm - shell

I need to set up a cron job that runs every 15 minutes between 8am and 8pm.
After googling, I decided to use it this way:
*/15 8-20 * * * /path/of/shellscript.sh
However, the cron is not getting triggered.
What should be the right expression?
Edits:
*/15 8-19 * * * /path/of/shellscript.sh
00 20 * * * /path/of/shellscript.sh
The cron executes. However the logs show this error:
Mailed 62 bytes of output but got status 0*0047
However, if I run the run the script manually, it executes fine!

Resolved by setting permissions to the sh file and the jar file
chmod 0777 shellscript.sh
chmod 0664 jarfile.jar

Related

Output not going to file when ran as a cron job

I have the following bash script:
clean-tmp.sh
#!/bin/bash
tmpreaper 1h /tmp --test > ./tmpreaper.log
When I run it in the terminal using ./clean-tmp.sh, it writes to the file ./tmpreaper.log.
I added the script to the list of cron jobs using crontab -e:
*/5 * * * * cd /home/cron-jobs && ./clean-tmp.sh
I then checked cron's logs and this entry is in there every 5 minutes:
Feb 19 00:45:01 ip-172-31-23-184 CRON[1475]: (ubuntu) CMD (cd /home/cron-jobs && ./clean-tmp.sh)
But it's no longer writing to ./tmpreaper.log.
What on earth am I doing wrong?
Just specify an absolute path for your file, like tmpreaper 1h /tmp --test > /var/log/tmpreaper.log
#Kacy: Little difficult to say without cron logs, you could have a look to cron logs(/var/log/cron etc).
0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /home/cron-jobs; ./clean-tmp.sh
May be some systems wouldn't allow time period as the way you tried, try once in above way and let us know then.

"getpwnam() failed" in /bin/sh only when called from cron

Here's the content of my crontab file:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="example#example.com"
*/5 * * * * sh /robot/1/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/2/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/3/master.sh
*/5 * * * * sh /robot/4/master.sh >/dev/null 2>&1
*/5 * * * * sh /robot/5/master.sh >/dev/null 2>&1
This is the error that keeps showing in /var/log/cron when it tries to run:
crond[669]: (sh) ERROR (getpwnam() failed)
If I run any of these files manually, they work without any issues.
What's wrong with the crontab file?
It surprises me that nobody has the correct answer to this. Today i faced exactly the same problem and google didn't help.
After 2 hours i found that when placing a file in /etc/cron.d the schedule line has to contain an extra option.....
I allways use this for my crontab -e
# Minute Hour Day of Month Month Day of Week Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) /my/fancy/script.sh
So it contains 6 items.
When placing this in a file inside /etc/cron.d the cron needs an extra option, being the user to run your fancy/script.
# Minute Hour Day of Month Month Day of Week Who Command
# (0-59) (0-23) (1-31) (1-12 or Jan-Dec) (0-6 or Sun-Sat) root /my/fancy/script.sh
This is documented in man crontab(5). For example https://linux.die.net/man/5/crontab . It says:
Jobs in /etc/cron.d/
The jobs in cron.d are system jobs, which are used usually for more than one user. That's the reason why is name of the user needed. MAILTO on the first line is optional.
The sixth position is reserved for username running the job. You specified a user called sh which is most probably not present on the machine.
simple answer
on your crontab you need to specify the USER to run the command
example to run as ROOT is:-
0,10,20,30,40,50 * * * * root /path_to_script/script_name
or to run as user FRED
0,10,20,30,40,50 * * * * fred /path_to_script/script_name
default with no USER specified is to run as user CRON and that user would not have permissions to execute the script
We can create cron jobs for system as well for individuals. The crontab in /etc/crontab specifically used for system cronjobs. So you need to specify the cronjob command executed by whom. In the question the username not specified. Hence the ERROR (getpwnam() failed) occurs. You can create user specific cronjobs in /var/spool/cron/username
NOTE:: Cron jobs are very useful but disastrous on failures!
Nothing is wrong with the crontab file (so long as by "my" crontab, you mean that it's a user crontab rather than a system crontab; otherwise, see other answer).
On the other hand, something is wrong with your system's directory service -- as configured, in Linux, with nsswitch.conf. Perhaps you're using a Kerberos-authenticated LDAP store, and your cron daemon doesn't have a Kerberos token to connect to it (or is sandboxed, as with SELinux, not to have network access); perhaps it's a file store that isn't readable by the user whose crontab is being run; perhaps some other odd and interesting thing is going on.
getpwnam() is a C library call that performs a lookup for the name of the currently-logged-in user. If your shell were bash, it would fall back to a name of I have no name! -- so this error means your sh implementation is something different. (If you want to run your scripts with bash, use bash, not sh).

El Capitan: CRON Job is unable to trigger a shell script

*/10 * * * * /bin/sh /dataScience/dat/JOB/forker.sh
Above is the cron job which does not execute. I have checked separately both the functioning of cron job and shell script which works fine. Even the below command works well.
/bin/sh /dataScience/dat/JOB/forker.sh
I have also checked the permissions of forker.sh which shows
-rwxr-xr-x 1 mango wheel 442 Nov 20 12:53 forker.sh
Am i missing something which restricts the cron job from triggering the script?
Generally the syntax for the job goes like:
minute hour dom month dow user cmd
In your case, the job might be for multiple user, it seems
*/10 * * * * /bin/sh /dataScience/dat/JOB/forker.sh
Which implies it should be run at the interval of 10 mins all the time. Which is not problematic.
Can you tell us if the following is giving you the output at all?
*/10 * * * * (echo "Printing."; echo "Logging." > /dataScience/dat/JOB/abc.txt | mail -s "Mailing" your_email)
Note: I wanted to comment this, since this is not exactly the answer but debugging, but I have reputation lesser than 50, hence couldn't comment.

How to run Bash Script in Crontab on Raspbian?

I have gone through all sorts of answers and replies up and down the Internet and nothing to seem to work for me. I want to simply run a bash script every minute using CRONTAB on Raspberry PI on Raspbian.
I have a script called autocon.sh and I simply entered into crontab as follows:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
...
1 * * * * root bash /home/pi/autocon.sh
BUT IT WON'T RUN.
What am I doing wrong?
I'm not sure where the 'root' part comes from, but I'm guessing you're wanting to run the script as root? If so, you need to put an entry into the root crontab, do so by running:
sudo crontab -e
This will open up the root user crontab for editing, anything run from said location will run with root priveleges.
Insert the following line:
* * * * * bash /home/pi/autocon.sh
That should do it :) The 1 in your script actually means 'run at 1 minute past the hour' and thus in your case, 1 minute past every hour - easy mistake! Replacing it with a * means every minute.
The syntax is:
minute - hour - day of month - month - day of week - command
Additionally, if you make your script executable, like so:
sudo chmod +x /home/pi/autocon.sh
You can omit the 'bash' command, and simply use:
* * * * * /home/pi/autocon.sh
And unless you're using the two lines at the top for something in particular, you can omit those too.
For clarity, Barmar's comment on my original post:
In per-user crontab files you don't put the username. But in
/etc/crontab you do.
To run a cronjob every minute, all the values must be stars. Your cronjob is set to run at 1 minute past the hour every hour.
It should be:
* * * * * root bash /home/pi/autocon.sh

CRON JOB is not running script

I'm trying to schedule a cron job here is my command:
*/5 * * * * USER -q /path/cron.php -mdefault 1
I'm trying to run this magento script every 5 minutes. I see the command being run when I open the cron log via grep CRON /var/log/syslog.
unfortunately the script never executes. I would appreciate any help.
Edit the crontab of the user you want to assign the job to:
sudo -u USER crontab -e
then in the crontab you can schedule the cron.php job in the following way:
*/5 * * * * php /path/to/cron.php

Resources