Shell Script not executing, added to crontab - linux

Here is my shell script, myscript.sh located in ~/bin
cd ../environment
. env/bin/activate
python3 office.py
The script office.py updates the database. I've tested and works with no issue. I used this command ./myscript.sh
Here is cronjob */5 * * * * cd ~/bin/myscript.sh added to crontab -e
When i check database, no changes. The cronjob isn't running? How do i solve?

You are not running the script but just trying to change directories, which will fail as myscript.sh is not a directory. You need to first cd ~/bin as you are using relative paths in your script and then run the script. Use this line:
*/5 * * * * cd ~/bin && ./myscript.sh
Also you may wanna check the syslog to check for cronjobs.
grep CRON /var/log/syslog
Have a look at this thread for more information on logging cronjobs.

Related

cron job + understanding where the script is run from

My script is in this directory: /home/sims/user1/live
but in cron it runs the script nelist_format.sh from here:
/home/sims even tho the script is not located there
my cron job looks like this:
48 9 * * * /home/sims/user1/live/nelist_format.sh >> /home/sims/user1/live/logs/nelist_format`date +\%Y\%m\%d`.log 2>&1
my work around is to change directory in my script
echo "$(pwd)"
cd /home/sims/user1/live #I have to change directory here else cron will run the file from /home/sims directory when I want to run it from cd /home/sims/user1/live
echo "$(pwd)"
But I am just trying to understand this process better.
Why is cron running my script from /home/sims when I was thinking it would be run from /home/sims/user1/live
Cron always runs jobs from the root of the user directory (/home/sims/). That becomes the current working directory.
You can, at the cron level, cd and then execute the command:
48 9 * * * cd /home/sims/user1/live && /home/sims/user1/live/nelist_format.sh >> /home/sims/user1/live/logs/nelist_format`date +\%Y\%m\%d`.log 2>&1

How to run cronjob in azure cloud bash shell

I'm trying to run cron job in azure cloud shell
but it is not working
This is my simple cron job
* * * * * /home/meet/clouddrive/temp.sh
where
cat /home/meet/clouddrive/temp.sh
#!/bin/bash
echo "meet" >> /home/meet/clouddrive/test.txt
pwd
/home/meet/clouddrive
meet [ ~/clouddrive ]$ ls
temp.sh
The script is located at /home/meet/clouddrive/temp.sh. However, the pwd command in the script outputs /home/meet/clouddrive, which suggests that the script is not in the current working directory. To fix this, you can either adjust the path to the script in the cron job, or you can change the current working directory in the script to the correct location.
Make sure that the script has the appropriate permissions to be executed. You can check the permissions for the script using the ls -l command, and you can change the permissions using the chmod command. For example, to make the script executable by the owner, you can run chmod u+x /home/meet/clouddrive/temp.sh.
Can you try running the script manually from the command line to see if it works fine?
I tried to reproduce the same in my environment and got the results like below:
I tried to run Cron job in azure cloud shell like below:
when I try crontab -l there is no job from here.
Then create crontab -e
Run */1 * * * * echo "this is a test" /home/imran123/testfile.txt
Then try to create a file using vi testfile.txt and I mention "This is test"
Then try to give execute permission like below:
chmod +x test.sh
Then when I executed cmd it run successfully like below:
crontab -l
cat testfile.txt

Shell script cronjob

I need a shell script that would run two .sh files which are in the directory: /opt/tomcat-latest/bin
#!/bin/sh
cd /opt/tomcat-latest/bin
./shutdown.sh
./startup.sh
Would this code achieve my goal? If so how do I make it on a cron job that runs every 2 hours? I have a Linux Centos VPS and DirectAdmin admin panel.
I think the easiest solution would be to run directly the commands with its full path from cron, instead of using a sh script.
Something like this in the crontab would work :
* */2 * * * /opt/tomcat-latest/bin/shutdown.sh && /opt/tomcat-latest/bin/startup.sh
That would run every 2 hours
you can edit crontab with crontab -e and check the crontab syntax here : https://fr.wikipedia.org/wiki/Crontab

Cron job does not run in RHEL

I'm running RHEL and I'm trying to set up a cron job to run a shell script every 5 minutes.
Following the directions here: https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/ch-Automating_System_Tasks.html#s2-configuring-cron-jobs
I have service crond start and chkconfig crond on. Then I edited /etc/crontab and added:
*/5 * * * * my-user /path/to/shell.sh
I did a chmod +x shell.sh. And I made sure to add a new line character at the end.
I'm expecting it to run every 5 minutes but it never executes.
What am I doing wrong?
Simply try to add the cronjob entry and check the script is working fine or not by taking the viewable output in the script.
echo "test time - $(date)" > script.sh
chmod +x script.sh
crontab -e
Then enter the cronjob as below,
*/5 * * * * sh /path/to/script.sh > /path/to/log.file
Check if the log is writing correctly. If its fine, better cross check the script that you are trying to execute via cron. Otherwise it will be a cron issue.

Downloading files with bash via cron

I have build a bash script that gets .tar.gz files from IMDb and writes to a log file, the script works when run on its own as I can see the folder with the files present, but when I run the script via cron it doesn't work. Would this be due to permissions? I have edited the sudo crontab file, but I'm not sure what else I need to do.
Try this solution:
Cronjob is a file that contains your job:
cat cronjob
* * * * * bash /path/to/script.sh >> /path/to/log.txt
Then you should set executable permission and start cron service:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
After that you should tell cron service to run cronjob file:
crontab cronjob
Your script should download a file.
If your script doesn't run you should run it from good path[full path], so your cronjob file would be something like this:
* * * * * /bin/bash /path/to/script.sh >> /path/to/log.txt

Resources