Cron job does not start [duplicate] - shell

This question already has answers here:
CronJob not running
(19 answers)
Closed last month.
I have a cron job that I want to execute every 5 minutes:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /scr_temp/scheduleSpider.sh
In /var/spool/cron/crontabs/root
The cron should execute a shell script:
#!/bin/sh
if [ ! -f "sync.txt" ]; then
touch "sync.txt"
chmod 777 /scr_temp
curl someLink
fi
That works fine from command line but not from cron. However the cron itself is startet but the script does not start.
I read about the path problem but I dont really understand it. I setup a cron that writes some env data to a file. This is the output:
HOME=/root
LOGNAME=root
PATH=/usr/bin:/bin
SHELL=/bin/sh
If I execute the env command in command line I get following output for PATH
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
What path do I have to set in my shell script?

Your $PATH is fine; leave it alone. On Ubuntu, all the commands you're invoking (touch, chmod, curl) are in /bin and/or /usr/bin.
How did you set up the cron job? Did you run crontab some-file as root?
It seems that /etc/crontab is the usual mechanism for running cron commands as root. On my Ubuntu system, sudo crontab -l says no crontab for root. Running crontab as root, as you would for any non-root account, should be ok, but you might consider using /etc/crontab instead. Note that it uses a different syntax than an ordinary crontab, as explained in the comments at the top of /etc/crontab:
$ head -5 /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Run sudo crontab -l. Does it show your command?
Temporarily modify your script so it always produces some visible output. For example, add the following right after the #!/bin/sh:
echo "Running scheduleSpider.sh at \`date\`" >> /tmp/scheduleSpider.sh.log
and see what's in /tmp/scheduleSpider.sh.log after a few minutes. (You can set the command to run every minute so you don't have to wait as long for results.) If that works (it should), you can add more echo commands to your script to see in detail what it's doing.
It looks like your script is designed to run only once; it creates the sync.txt file to prevent it from running again. That could be the root (ahem) of your problem. What that your intent? Did you mean to delete sync.txt after running the command, and just forgot to do it?
root's home directory on Ubuntu is /root. The first time your script runs, it should create /root/sync.txt. Does that file exist? If so, how old is it?
Note that curl someLink (assuming someLink is a valid URL) will just dump the content from the specified link to standard output. Was that your intent (it will show up as e-mail to root? Or did you just not show us the entire command?

First: you can substitute the first field with */5 (see man 5 crontab)
Second: have cron mail the output to your email address by entering MAILTO=your#email.address in your crontab. If the script has any output, it'll be mailed. Instead of that, you may have a local mailbox in which you can find the cron output (usually $MAIL).

A better syntax for you CRON is
*/5 * * * * /scr_temp/scheduleSpider.sh
Also, check the authority of your scheduleSpider.sh file. Cron runs under a different user than the one you are likely executing your program interactively, so it may be that cron does not have authority. Try chmod 777 for now, just to check.

I suggest to:
check that /scr_temp/scheduleSpider.sh has executable bit
set PATH properly inside your script or use absolute path to command (/bin/touch instead of touch)
specify absolute path to sync.txt file (or calculate it relatively to script)

Have you added the comand via crontab -e or just by editing the crontab file? You should use crontab -e to get it correctly updated.

Set the working directory in the cron script, it probably doesn't execute the things where you think it should.

You should add /bin/sh before the absolute path of your script.
*/5 * * * * /bin/sh /scr_temp/scheduleSpider.sh

Related

Some commands not working in the script running from the crontab -e

I'm running a script from crontab in which I want to set the symbolic link for npx. It does some other things which are dependent on the npx command itself. Its running the script as expected on the giving time interval, but its giving me no result for command which npx or whereis npx. When I try to run the script from terminal directly these commands does generate the correct path.
Note that, crontab I'm using is under the root user privilege, i.e set with sudo crontab -e and verified with echoing whoami inside the script which generate 'root')
By default, crontab will run your cron jobs using sh which might be the reason you are getting no results.
Try to explicitly change the shell to your default shell by adjusting the crontab entry:
*/30 * * * * /bin/bash -c "/my_script.sh"
In this case I changed it to bash, you can change it to your desired shell.

What is the result of running 'crontab *'?

I've accidentally ran 'crontab *'. Afterwards, crontab -l and, possibly, crontab -e stopped working properly.
Can someone more knowledgeable in cron help me explain what would happen if 'crontab *' command is ran?
I ran crontab -l | grep * and very few cronjobs showed up. I also ran crontab -e in order to edit the crontab file and it gives me the "Error detected while procedssing 'pathway'" message and "E518: Unknown option: foldmehod=marker" error. It brings me to /tmp/crontab* where * denotes an attempt to create a cron file at tmp folder.
I expect the output of crontab -l | grep "name" to output something but the output doesn't show anything. I suspect it's me running crontab *.
In short, the result would be crontab attempting to install every file that * would have expanded to. Since you did not specify a user with -u first, it would have defaulted to whatever user you were executing the command as. The good news is this only messed up your private crontab.
Cron uses two different crontabs; a system-wide crontab at /etc/crontab and the private, user-specific crontabs at /var/spool/cron. When you use crontab, for example as root, to install a new file, those changes are actually made to /var/spool/cron/root, not the global crontab found at /etc/crontab.
Thus the damage is mitigated to the private crontab of whichever user ran that command, which you can most likely safely remove and rebuild if necessary; but whatever is inside of that private crontab is going to be junk.

Why is this rsync not working?

I'm not having SSH access, only cronjob access.
I've written a copy.sh script in order to rsync the folders, but it does not happen. Why?
OLDDIR="/var/www/web46/html/magento-v2"
NEWDIR="/var/www/web46/html/magento-v4"
rsync -au $OLDDIR/media/ $NEWDIR/media >> log.txt
# rsync -au $OLDDIR/app/code/ $NEWDIR/app/code
# rsync -au $OLDDIR/app/design/ $NEWDIR/app/design
# rsync -au $OLDDIR/app/etc/modules/ $NEWDIR/app/etc/modules
# rsync -au $OLDDIR/app/locale/ $NEWDIR/app/locale
# rsync -au $OLDDIR/js/ $NEWDIR/js
# rsync -au $OLDDIR/skin/ $NEWDIR/skin
This is the target folder:
This is the destination folder AFTER the script has run:
What am I doing wrong?
Update
For any shell script running from a cron:
Use full paths. (Both in the script, and in the crontab referencing the script)
Ensure the script is made executable and that the correct user has permissions to run. (see
Regarding Magento:
Make sure you use crontab syntax applicable to magento.
See this example setup from this answer: https://magento.stackexchange.com/a/63717
*/5 * * * * www-data /bin/sh /path/to/magento/cron.sh cron.php -m=default
*/5 * * * * www-data /bin/sh /path/to/magento/cron.sh cron.php -m=always
Consider posting your question in http://magento.stackexchange.com if these basic steps don't help.
NOTE: Most tutorials / help guides / help forums, will assume, rightly or wrongly, the default position that you are a system admin, given that you are troubleshooting an admin level task. Please specify your exact constraints.
Refer to Check your existing crontab
troubleshooting page
Check your existing crontab
To verify whether or not your crontab is set up:
Log in to your Magento server. As a user with root privileges, see if
a crontab is already set up.
crontab -u <Magento file system owner name> -l
For example, on CentOS
crontab -u magento_user -l
If no crontab has been set up for the user,
the following message displays:
no crontab for magento_user
See one of the following sections for a
solution to your issue.
Solution: crontab not set up
To verify your cron jobs are set up properly, see Set up cron jobs.
This sample script adds full paths and a shebang line to your rsync script. Note: you may need to be able to test this outside of the cron to pinpoint the issue :
#!/bin/bash
echo "At least I know the script ran up to here" > /var/log/myscriptran.log
OLDDIR="/usr/share/full-path/something/magento-v2"
NEWDIR="/usr/share/full-path/something/magento-v4"
/usr/bin/rsync -au "$OLDDIR/media/" "$NEWDIR/media" 2>> /var/log/rsync_cron.log
Additionally: If for some reason you need to run in the same environment, you can always add source /User/your/home/folder/path/.profile (replace with your applicable profile path).
Crontab runs in it's own environment, you need to assume it has no knowledge of you, your home folder, relative paths, your ssh certificates location, any common environment variables / settings that you may not even be aware of, etc.
What it will always understand is full paths, and it will execute, executable files, it has permission to run.

Running a Shell Script as a Cronjob

I have a written a shell script to automate a build process.
The script checksout some code from an SVN repo, compiles and builds the code before extracting the built binary files and storing these in a central location.
I can manually execute the script ./autobuild.sh and it runs perfectly. There are a few sudo commands executed throughout the script, but I echo the password through for the first sudo command and the password holds for the entire time:
echo mypassword! | sudo -S make clean
When I add executing the script as a crontab it fails to complete all the tasks. I've tried to add it as a cronjob for the normal and root users.
Running crontab -e on my normal user account, I want the script to run at ten past midnight every day:
10 0 * * * /home/username/autobuild.sh
Also running a 32-but Cent OS 7 install with all the latest updates installed.
Can anyone provide any suggestions as to why it might work manually but not when run through a cron?
Try this
10 0 * * * /bin/bash /home/username/autobuild.sh
Step 1. find bash path
:~# whereis bash
Output
bash: /usr/bin/bash
step 2. create sh file add the line on top replace with your bash path
#!/usr/bin/bash
step 3. Make the script executable with command chmod +x .
step 4. add cron like this in for every minute to test
crontab -e
*/1 * * * * /usr/bin/bash ~/backup.sh >>test.log

Cron job can't load gem

I have a ruby script that connects to an Amazon S3 bucket and downloads the latest production backup. I have tested the script (which is very simple) and it works fine.
However, when I schedule this script to be run as a cron job it seems to fail when it loads the Amazon (aws-s3) gem.
The first few lines of my script looks like this:
#!/usr/bin/env ruby
require 'aws/s3'
As I said, when I run this script manually, it works fine. When I run it via a scheduled cron job, it fails when it tries to load the gem:
`require': no such file to load -- aws/s3 (LoadError)
The crontab for this script looks like this:
0 3 * * * ~/Downloader/download.rb > ~/Downloader/output.log 2>&1
I originally thought it might be because cron is running as a different user, but when I do a 'whoami' at the start of my ruby script it tells me it's running as the same user I always use.
I have also done a bundle init and added the gem to my gemfile, but this doesn't seem to have any affect.
Why does cron fail to load the gem? I am running Ubuntu.
As mentioned here https://coderwall.com/p/vhv8aw you can simply try
rvm cron setup # let RMV do your cron settings
Make sure that you make copy of your crontab before running this command
If you're running it manually and it works you're probably in a different shell environment than cron is executing in. Since you mention you're on Ubuntu, the cron jobs probably execute under /bin/sh, and you're manually running them under /bin/bash if you haven't changed anything.
You can debug your environment problems or you can change the shell that your job runs under.
To debug, There are several ways to figure out what shell your cron jobs are using. It can be defined in
/etc/crontab
or you can make a cron job to dump your shell and environment information, as has been mentioned in this SO answer: How to simulate the environment cron executes a script with?
To switch to that shell and see the actual errors causing your job to fail, do
sudo su
env -i <path to shell> (e.g. /bin/sh)
Then running your script you should see what the errors are and be able to fix them (rubygems?).
Option 2 is to switch shells. You can always try something like:
0 3 * * * /bin/bash -c '~/Downloader/download.rb > ~/Downloader/output.log 2>&1'
To force your job into bash. That might also clear things up.
You may also explicitly set your Gem path:
GEM_HOME="/usr/local/rvm/gems/ruby-1.9.2-p290#my-special-gemset"
in a non cron environment execute echo $PATH, copy the path and paste it into your crontab, before your command:
echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
and inside crontab:
PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
0 3 * * * ~/Downloader/download.rb > ~/Downloader/output.log 2>&1
Add this at the beginning of your cron
PATH="/home/user/.rvm/gems/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4#global/bin:/home/user/.rvm/rubies/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4/bin:/home/user/.rvm/gems/ruby-2.1.4#global/bin:/home/user/.rvm/rubies/ruby-2.1.4/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/home/user/.rvm/bin:/usr/local/sbin:/usr/sbin:/home/user/.rvm/bin:/home/user/.local/bin:/home/user/bin"
GEM_HOME='/home/user/.rvm/gems/ruby-2.1.4'
GEM_PATH='/home/user/.rvm/gems/ruby-2.1.4:/home/user/.rvm/gems/ruby-2.1.4#global'
MY_RUBY_HOME='/home/user/.rvm/rubies/ruby-2.1.4'
IRBRC='/home/user/.rvm/rubies/ruby-2.1.4/.irbrc'
RUBY_VERSION='ruby-2.1.4'
I've tried all the solution above, none of them worked until I tried;
0 12 * * * /bin/bash -l -c 'ruby /Users/simon/Desktop/script.rb'

Resources