I just made a little .sh script which just downloads a .txt file from the web. I want this to be run every two minutes.
getfeed.sh:
#!/bin/sh
FILENAME=`date +"%Y%m%d%H%M"`.txt
cd feedhistory
/usr/bin/wget -O $FILENAME http://www.example.com/link/to/file.txt
Crontab:
*/2 * * * * /bin/sh /var/www/src/cron/getfeed.sh
Syslog entry:
Sep 24 XX:XX:XX servername CRON[10451]: (root) CMD (/bin/sh /var/www/src/cron/getfeed.sh)
The .sh file itself works fine and the syslog shows Cron is doing something with the file. But there is nothing downloaded...
Thanks in advance
Henry
Usually cron runs the command in the user's home directory. Your cron job doesn't specify a user, so it will be executed by root.
As your $FILENAME does not contain a path, the file should be downloaded to the /root/ directory of your system.
If you want them to be somewhere else, you should either set a user for the cron job, or set an absolute path for the download destination.
Related
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.
I am new to Linux and have a problem that I have tried to look online and could not find a solution
I have 4 scripts that are running in cron.d 3 of them I set to run every minute and they are fine and logging into the output files but the last one should run at 1:00 am but it will not.
-rw-r--r-- 1 root root 390 Jul 29 14:03 Test
* * * * * root /usr/sa/dir1/dir2/script1.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
* * * * * root /usr/sa/dir1/dir2/script2.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
* * * * * root /usr/sa/dir1/dir2/script3.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
0 1 * * * root /usr/sa/dir1/dir2/script4.sh >> /usr/sa/dir1/logs/fileoutput 2>&1
I checked the permission and all seems to be fine as the same script from cron.d file are running as I can see entries from cron that are executed in /var/log/messages and same from the log files.
Things I have tried till now and worked
IF I vim the file and change for the 4th script to run every minute it runs fine.
IF I vim the file and change for the 4th script to run during the day it runs.
IF I include the script under crontab of root user and it runs ok.
IF I run script form the command line it runs ok.
I can not figure out why viming the file in cron.d the script will be executed by cron.
Thank you in advance for the help
Things I have tried till now and worked
IF I vim the file and change for the 4th script to run every minute it runs fine.
IF I vim the file and change for the 4th script to run during the day it runs.
IF I include the script under crontab of root user and it runs ok.
IF I run script form the command line it runs ok.
I can not figure out why viming the file in cron.d the script will be executed by cron.
I don't see errors in /var/log/messages
Are you commiting this crontab by just editing the raw file or are you using crontab <my-new-crontab>? If you aren't then try the latter. :)
I end it up adding the scripts to the crontab of the user root and seems to be ok.
I am trying to run a shell script on crontab on Ubuntu platform. I have tried googling and other links but nothing has helped so far.
This is my crontab:
*/2 * * * * sudo bash /data/html/mysite/site_cleanup.sh
This is the content of my sh file:
#!/bin/sh
# How many days retention do we want ?
DAYS=0
# geting present day
now=$(date +"%m_%d_%Y")
# Where is the base directory
BASEDIR=/data/html/mysite
#where is the backup directory
BKPDIR=/data/html/backup
# Where is the log file
LOGFILE=$BKPDIR/log/mysite.log
# add to tar
tar -cvzf $now.tar.gz $BASEDIR
mv $now.tar.gz $BKPDIR
# REMOVE OLD FILES
echo `date` Purge Started >> $LOGFILE
find $BASEDIR -mtime +$DAYS | xargs rm
echo `date` Purge Completed >> $LOGFILE
The same script runs from a terminal and gives the desired result.
Generic troubleshooting for noninteractive shell scripts
Put set -x; exec 2>/path/to/logfile at the top of your script to log all subsequent commands to a file as they're run. If this doesn't work, you'll know that your script isn't being run at all; if it does, you'll know where it fails and how.
If this is a personal crontab
If you're running crontab -e as a user (without sudo), then the crontab being modified is one for commands run with that user's permissions. Check that file permissions allow that user to modify the content in question (which, if these files are in a cgi-bin directory, may require being run by the same user as the web server).
If your intent is to have commands run as root, rather than as your own user, be sure you use sudo when editing the crontab to edit the system crontab instead (but please take care as to your script's correctness in this case -- carelessness such as missing quotes or lack of appropriate precautions in xargs usage can cause a script to delete the wrong files if malicious filenames are created):
sudo crontab -e ## to edit the system (root) crontab
...or, if you're cleaning up files owned by the apache user (for example; check which account is correct for your own operating system and web server):
sudo -u apache crontab -e ## to edit the apache user's crontab
Troubleshooting for a system crontab
Do not attempt to put a sudo command within the commands run by cron; with sudo's default configuration, it requires a TTY (a keyboard and screen) to be attached to a session in order to run. Thus, your crontab line should not contain sudo, but instead should look like the following:
*/2 * * * * bash /data/html/mysite/site_cleanup.sh
Your issue is likely coming from the sudo call from your user level cron. Unless you've gone through and edited the bashrc profile to allow that script to run without sudo it'll hang up every time.
So you can lookup how to run a script with no password by modifying the bashrc profile, remove the sudo call if you aren't doing something in your script that calls for Super User permissions, or as a last ditch, extremely bad idea you can call your script from root's cron by doing sudo crontab -e or sudo env EDITOR=nano crontab -e if you prefer nano as your editor.
try to add this line to the crontab of user root and without the sudo.
like this:
*/2 * * * * bash /data/html/mysite/site_cleanup.sh
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
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