Trying to run script through crontab which won't work - linux

I'm having issues getting my crontab to run I have the following line added to my crontab -e but it won't start. The command runs fine if I run it manually.
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py
Not getting any error messages and can't see the process when I run top or grep for it.

Usually this happens because the cron environment is different from your own. Make sure your start.py script uses full paths to any referenced files or external scripts. Make sure that your start.py script does not rely on environment variables that you have in your shell but it may not. Try piping the cron output to a mail command so you can see what it is doing, like so:
0 */3 * * * cd /home/sam/p/ && /usr/bin/python3.5 start.py | mail -s "cron output" myself#example.com

An easier way to troubleshoot this is to write a wrapper shell script and send the output to a log file.
Create file python_start_cron.sh with contents
#!/bin/bash
cd /home/sam/p/ && /usr/bin/python3.5 start.py
Set the execute bit on this script script and make sure the script works manually
Modify the cronjob as shown below
0 */3 * * * python_start_cron.sh >/tmp/python_start_cron.log 2>&1
After cron executes, check the contents of the log file to ascertain the cause of the problem.

Related

Running `wsgetmail` service via Crontab job CentOS 6

I want to run the below via Crontab job and not working but when put them in sh file and run the sh manually it works fine.
Sh file path: /opt/etc/rt4/test.sh and the content as below:
wsgetmail --config=account01.json
wsgetmail --config=account02.json
Running manually:
sh /opt/etc/rt4/test.sh it works fine.
Crontab:
*/1 * * * * /opt/etc/rt4/test.sh
Crontab runs this file but those commands are not working.
I have other Crontab jobs and they are working fine as intended.
The crontab and terminal are two different environments, the wsgetmail perl module command is recognizable for terminal but to make it recognizable for corntab we have to add the full path to the module (wsgetmail) in the shell script.
in this case test.sh should looks like this:
#!/bin/bash
/usr/local/bin/wsgetmail --config=account01.json
/usr/local/bin/wsgetmail --config=account02.json
Running manually: sh /opt/etc/rt4/test.sh it works fine.
Crontab: */1 * * * * /opt/etc/rt4/test.sh
Those are not the same thing, as 1st line shebang, and chmod a+x test.sh, will affect the behavior.
Either remove "sh" when running manually, or prepend it to the cron command.
Run $ id, and determine if that's different
from how the cron command runs, perhaps by
having cron log id output.
Running as yourself manually,
versus as root (uid=0) under cron,
can change the behavior of that command.
Numerous other things are different
under cron, such as lack of a pty.
Take a look at $ env | sort manually.
Then run it under crond, and note the huge difference.
Pay special attention to PATH.
It is likely to be much shorter under cron,
and that can lead to "command not found"
diagnostics.
But you chose not to share any diagnostic
error messages with us,
so coming up with a definitive diagnosis
of this amounts to a mind reading exercise.

How to use cron on a simple script

I want to use cron for execute a script periodically. I want to try a simple script first but it does not work.
This is my script (scritp.sh) which permission are 700:
#!/bin/sh
clear
echo "Hello!"
mkdir Hello
And this is the crontab file when I edit it with the command crontab -e:
SHELL=/bin/sh
* * * * * /home/padro/Documents/script.sh
EDIT:
I have that script on /home/padro/Documents folder. What I do after it is execute the command crontab -e for modify the cron file. In this file I put the shell that I want SHELL=/bin/sh and also the cron schedule expression * * * * * /home/padro/Documents/script.sh. This schedule teorically run the script every minute. Finally I save the file and when a minute passes I can't see the echo of the script on the terminal.
EDIT2:
I have added mkdir hello, because I don't know if the echo of the script is shown on the terminal. But the hello directory is never created.
Any output generated by a program called from cron will by default be emailed to the user owning the crontab (assuming local delivery of mail messages is possible). So I'd suggest that you look in your inbox on the local machine.
To save the output into a file, use a redirection in the crontab, or arrange for the script to write its output to a file.
Jobs started by cron does not run with a terminal, so you should not expect to see your terminal being cleared every minute by running this script through cron.
The Hello folder should have been created in the working directory used by the script (possibly your home directory). To make absolutely sure you know where the script's working directory is, use cd in the script to move to the correct location.
I do not have enough reputation to add comment.
My humble comment would be.
Is the cron file you mentioned via root?
cos chmod 700 a file would be only be executed by owner.
If you are using redhat linux, the user account you use on the first log in is user rights NOT root.
Reference link to a cheat sheet.
su - root
system will prompt root password
crontab -e
* * * * * /home/padro/Documents/script.sh
You can even run a test script, which I did encounter the similar situation as you when I first learnt scripting into your crontab-
* * * * * date > export/home/padro/Documents/testing.txt
If you could, restart the server.
Check if your directory is correct using the command
pwd in linux/unix.
I hope my comment based on my recent learning have helped you.
Edit 1: Remove clear in your script. Thanks...
Edit 2: I believe your Hello folder is created at the core of the root folder try looking for it... or the home directory of the user...

Simple--running shell script with a cronjob

Simple Question:
I want a cron job to run a script every minute. This script (script.sh) generates a .zip file of all the files in the directory.
I have written the script in a file called script.sh:
if [ -z "$(ls -A /var/www/html/convo_files)" ]; then
exit
else
zip -rj zipped.zip /var/www/html/convo_files/*
fi
Successfully creates .zip file when I run it with command ./script.sh
My crontab script is:
*/1 * * * * /var/www/html/convo_files/script.sh
Gives message in mail (which looks correct):
updating: crap.txt (deflated 89%)
updating: script.sh (deflated 36%)
Cronjob file is within same directory as script.sh btw, yet no .zip file is created with cronjob. Really not sure how to solve this dilemma.
Have you tried out the following cron job ?
*/1 * * * * /path/to/your/script.sh
This should execute your script every minute.
If you have problems executing the script make sure you have the "executable flag" set (chmod +x /path/to/your/script.sh)
If you still have errors you should take a look at your syslog. On most Linux systems the file /var/log/syslog is the currect one but this can differ from system to system.
Command in crontab file needed to be changed:
* * * * * cd /var/www/html/convo_files && /var/www/html/convo_files/script.sh
Problem was working directory wasn't set right (https://unix.stackexchange.com/questions/38951/what-is-the-working-directory-when-cron-executes-a-job )
For beginners like myself, make sure to look at your mail directory/log files to see your errors!
Note, to go into your mail directory:
cd /var/spool/mail
tail root
Thank you to everyone who answered and helped!

crontab failing to run a shell script that wgets a url at a specified interval

I have the following command inside a shell script at /home/ubuntu/wget_my_url.sh
the conent of ping_my_url are as follows -
wget -O - -q -t 1 http://www.someurl.com/baseurl/
There is no line break in the above file.
I did chmod +x wget_my_url.sh
Inside my /etc/crontab I have the following - */1 * * * * /home/ubuntu/wget_my_url.sh
There is a line break after the above line inside the crontab file.
When I run manually wget_my_url.sh, I get the desired result, but inside a cron tab, it does not run.
Please let me know, whats wrong with it.
Thanks.
What do you mean with the "desired result" ?
Your script just print choses on the screen (the standard out), but, the scripts on the crontab doesn't have a screen to print.
So, I think that it's running normally but you can't realize cause you don't see the script's output.
Instead of just print, why don't you print on a file making something like
wget -O - -q -t 1 http://www.someurl.com/baseurl/ > /home/ubuntu/OUT_FILE
?
Like that you'll be able to check if it works just checking if the file /home/ubuntu/OUT_FILE exists and you'll be able to use the script's output too.

Cronjob to run hourly

I have written a small bash script which clears the logs (say that script name is clearLogs.sh).
My task: To run the bash script on an hourly basis to clear the logs
What I have done so far: I have created a symbolic link of my bash script and placed it in /etc/cron.hourly.
example -> cd /etc/cron.hourly
ln -s /home/sam/clearLogs.sh clearLogs.sh
Now, an hour has passed but the logs have not been deleted. If I run a script standalone it works as expected. Can you guys please let me know what I am doing wrong here.
1.in clearLogs.sh add line: RUN_PATH=/you/script/path cd $RUN_PATH
2.cat "* */1 * * * /you/script/path/clearLogs.sh" >> $HOME/crontab.txt
3.crontab $HOME/crontab.txt
4.crontab -l,look all crontab job

Resources