why my crontab is not executing my shell script? - shell

where to check the logs of cron job?
I did pgrep cron it gave 2358
What does it mean?
0 02 * * * /feedbatch/application/scripts/baselineUpdate.sh
0 13 * * * /feedbatch/application/scripts/baselineUpdate.sh
#0,10,20,30,40,50 * * * * /feedbatch/application/scripts/partialUpdate.sh
*/2 * * * * /feedbatch/application/scripts/partialUpdate.sh
15 0 * * * /usr/sbin/logrotate -s $HOME/logrotate.status -f $HOME/endeca-logrotate.conf
16 0 * * * /usr/sbin/logrotate -s $HOME/outputrotate.status -f $HOME/endeca-outputrotate.conf

Your scripts are in this location -- /feedbatch/
Please check whether a directory named feedbatch is there in / . and whether a normal user has permission to run the scripts inside it if the directory exists. Try to put all your scripts in a new bin folder.. thats a good practice..
Other scripts are in /usr/sbin/....
Here also the permission may be an issue.
Try to put all these inside the root crontab. So that there wont be any permission issues.
Also check if your username is there in /etc/cron.allow and /etc/cron.deny.Dont worry If these two files are not present. But if these files are there, please make sure that your name is there in cron.allow and not there in cron.deny
to access the root crontab do the following
su - root
crontab -e

The cron daemon will log to default system logger. So it will be logged to messages or syslog.
You can configure the rsyslog to log all cron log output separate file. This is a bit depending of your linux distribution. As follow an example to write cron log to separate file for Ubuntu12.04:
edit /etc/rsyslog.d/50-default.conf (uncommand the cron.* line)
...
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none -/var/log/syslog
#cron.* /var/log/cron.log
#daemon.* -/var/log/daemon.log
...
restart cron daemon.
Your cron output should be logged to /var/log/cron.log now.
That should get your an better overview about "cron doing".

Related

cron job not running as root user in digital ocean droplet

I created this test bash script
#! /bin/bash
echo "hello"
mkdir "/karan/washere"
cron job i created, i want to run this cron job to run every min and want the log
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log
I am signed in the droplet as root user
I also have given the permission for the script, using
sudo chmod u+x test.sh
I tried to log the syslog using
sudo grep CRON /var/log/syslog
but didn't show there also,
let me know if you need any more info or other context,
cron uses sh to interpret the commands, not bash. sh has less features and the &> is not valid in sh.
A better way for your line in the crontab would be:
1 * * * * /user/local/bin/bash /root/test.sh >> /root/crontest.log 2>> /root/crontest.err
This will append to the logging instead of overwriting, and it will separate log from errors.
If you want, however, you can force cron to use bash instead of sh. Your crontab should then be:
SHELL=/bin/bash
#testing if cron job workes or not
1 * * * * /user/local/bin/bash /root/test.sh &> /root/crontest.log

Adding a cronjob for a different user from a script?

I have a script that is executed by the user "www-data" that requires adding a couple of cronjob that run under the user "test".
#!/bin/bash
echo "* * * * * test /usr/local/bin/test.sh" > /etc/cron.d/myjob
Fails because obviously www-data has no permissions to write to that folder and I doubt changing the folder permissions is a good idea.
#!/bin/bash
(crontab -u test -l ; echo "* * * * * /usr/local/bin/test.sh") | crontab -u test -
Gives must be privileged to use -u despite adding www-data ALL=(root) NOPASSWD: /usr/bin/crontab to visudo.
It must be noted its not possible to ssh into the server to manually add the job. I'm trying to do this through a web based interface that allows user to select some values, and then run a script on the box to set it up.
How can I have "www-data" create a cronjob for the user "test"?
In order for the sudo privileges you added to have any effect at all, you have to actually use sudo.
#!/bin/bash
( crontab -u test -l;
echo "* * * * * /usr/local/bin/test.sh") |
sudo crontab -u test -
# ^^
Basicly you cannot do this without superuser access because this is a security violation.
Since we're most probably talking about shared hosting, probably there is no way to configure sudo rules or create files inside /etc/cron.d/, but there may be some UI to configure cronjobs.
#tripleee
Thanks for putting me on the right path. Putting sudo infront of both commands fixed the problem.
#!/bin/bash
( sudo crontab -u test -l;
echo "* * * * * /usr/local/bin/test.sh") |
sudo crontab -u test -

Crontab not working (standard troubleshooting done)

SHELL=/bin/bash
* * * * * /home/VI/vserver/jira_extractor/bash_scripts/cronbg.sh
I edit crontab with crontab -e as a local user.
Cron is running root 7296 1 0 17:28 ? 00:00:00
/usr/sbin/cron -f
My script runs when using exactly the same path as specified in the
crontab
There is an empty line at the end of the crontab script.
I have specified that crontab will run in bash shell
Any hints on trouble shooting will be greatly appreciated
In my case, adding the environment variables in the bash scripts to my crontab file solved the problem
Does the user allow to run cron?
Print out /etc/cron.allow & /etc/cron.deny.
Crontab troubleshooting will require to check log file /var/log/syslog. Assuming that user is allowed to run cron, then there will be an entry when your schedule job is run.
Ref: https://help.ubuntu.com/community/CronHowto

How to run a script from crontab and avoid "LoadError"

I want run my script from the crontab on Mac OS, but I'm getting an error:
ruby: Operation not permitted -- /Users/vitalii/Desktop/Home/update/update.rb (LoadError)
My preferences for the cron task and settings are created using rvm cron setup:
#sm start rvm
PATH="/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1/bin:/Users/vitalii/.rvm/gems/ruby-2.4.1#global/bin:/Users/vitalii/.rvm/rubies/ruby-2.4.1/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/vitalii/.rvm/bin"
GEM_HOME='/Users/vitalii/.rvm/gems/ruby-2.4.1'
GEM_PATH='/Users/vitalii/.rvm/gems/ruby-2.4.1:/Users/vitalii/.rvm/gems/ruby-2.4.1#global'
MY_RUBY_HOME='/Users/vitalii/.rvm/rubies/ruby-2.4.1'
IRBRC='/Users/vitalii/.rvm/rubies/ruby-2.4.1/.irbrc'
RUBY_VERSION='ruby-2.4.1'
#sm end rvm
* * * * * ruby /Users/vitalii/Desktop/Home/update/update.rb >> /Users/vitalii/Desktop/logfile.txt 2>&1
I gave each file the rights to execute with chmod 777, but there are no changes and the error is repeated.
The contents of the file update.rb are
puts 'Hello, World!!!'
Can someone tell me what's going on and what I'm doing wrong ?
I found solution, since I use Mojave.
I need to make additional settings in the system. Who would have thought ...
enter link description here
This turns out to be a problem, since you need to allow permissions for cron.
And correct run command for crontab -e, this
* * * * * /bin/bash -l -c 'ruby /Users/vitalii/Desktop/Home/update/update.rb'
There is a little difference when setting crontab job via crontab -e or putting them in /etc/crontab
$ crontab -e
# m h dom mon dow command
* * * * * echo ok
$ nano /etc/crontab
# m h dom mon dow user command
* * * * * root echo ok
Is this the case?

Can I create a crontab "on the fly" in a script?

I have a script that creates some temporary files that need to remain in place for quite some time. I want the user who executes the script to be able to create their own custom crontab that removes these files at a later time.
To test, I've just simply tried to setup a simple crontab using the command-line exclusively, but I'm not sure if this is even possible.
From the command-line I type the following:
$ crontab 1 * * * * $(mkdir -p ~/Desktop/CronSuccess)
I get the error: crontab: 1: No such file or directory
Is there anyway to have a script create a fully functional crontab on the fly?
Read the manpage for crontab
You can't do what you're trying to do.
You can however have crontab read the entries from a file.
e.g.
echo 'crontab 1 * * * * mkdir -p ~/Desktop/CronSuccess' >mycrontab
crontab mycrontab
Be aware that this will not append to the users crontab, it will replace the existing crontab of the user
with what's in the mycrontab file.
Surrounding the mkdir command with $() would be wrong.
You might also use at if it suits your needs:
e.g.
echo 'mkdir -p ~/Desktop/CronSuccess' | at now + 10 hours

Resources