How to execute a script using crontab - bash

I'm trying to execute a particular script located in /logann/myFile through crontab file.
I've already tried a lot of ways but no one makes any results.
I add in crontab file this line to call my script
26 10 * * * root cd /logann && ./apagar_temp_tomcat
(The time I change to test)
And my script is
#!/bin/sh
rm /tmp/tomcat7-tomcat7-tmp/*.tmp
rm /tmp/tomcat7-tomcat7-tmp/*.xml
I just want to execute this script. This script will delete every .tmp/.xml files in the folder tomcat7-tomcat7-tmp.
Any ideas?
Thank you guys!

cron jobs will run in their own shell. So you can't expect to see tedvs on your console.
Try to write something in file, for example
* * * * * echo tedvs > file_in_your_directory.txt

Just make one function in file and call that file or function in cron as per your time interval.
I did this in PHP
shell_exec(\'(crontab -l ; echo "* * * * * curl www-data http://localhost/sp/delete.php") | crontab -\');

Related

Append text to a file every minute using crontab in mac os x is not working

I'm trying to run a cron command every minute in mac os. Below is the code for my .sh file named logit.sh
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/Users/myname/Desktop
printf "\nThis is a new line to your document" >> file.txt
I made the file executable with below command
sudo chmod +x /Users/myname/Desktop/logit.sh
Below is the code for my crontab. I need to run the command every minute to append the text to the text file to check whether cron is working properly.
* * * * * sh /Users/myname/Desktop/logit.sh
I tried the below command as well, it doesn't work
* * * * * /Users/myname/Desktop/logit.sh
However, if I give the below command in the terminal, it works fine.
sh /Users/myname/Desktop/logit.sh
What am I missing here?
The problem was with the syntax for the crontab file. Below is the code which worked for me.
* * * * * cd /Users/myname/Desktop && ./logit.sh

Output not going to file when ran as a cron job

I have the following bash script:
clean-tmp.sh
#!/bin/bash
tmpreaper 1h /tmp --test > ./tmpreaper.log
When I run it in the terminal using ./clean-tmp.sh, it writes to the file ./tmpreaper.log.
I added the script to the list of cron jobs using crontab -e:
*/5 * * * * cd /home/cron-jobs && ./clean-tmp.sh
I then checked cron's logs and this entry is in there every 5 minutes:
Feb 19 00:45:01 ip-172-31-23-184 CRON[1475]: (ubuntu) CMD (cd /home/cron-jobs && ./clean-tmp.sh)
But it's no longer writing to ./tmpreaper.log.
What on earth am I doing wrong?
Just specify an absolute path for your file, like tmpreaper 1h /tmp --test > /var/log/tmpreaper.log
#Kacy: Little difficult to say without cron logs, you could have a look to cron logs(/var/log/cron etc).
0,5,10,15,20,25,30,35,40,45,50,55 * * * * cd /home/cron-jobs; ./clean-tmp.sh
May be some systems wouldn't allow time period as the way you tried, try once in above way and let us know then.

How to run Bash Script in Crontab on Raspbian?

I have gone through all sorts of answers and replies up and down the Internet and nothing to seem to work for me. I want to simply run a bash script every minute using CRONTAB on Raspberry PI on Raspbian.
I have a script called autocon.sh and I simply entered into crontab as follows:
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
...
1 * * * * root bash /home/pi/autocon.sh
BUT IT WON'T RUN.
What am I doing wrong?
I'm not sure where the 'root' part comes from, but I'm guessing you're wanting to run the script as root? If so, you need to put an entry into the root crontab, do so by running:
sudo crontab -e
This will open up the root user crontab for editing, anything run from said location will run with root priveleges.
Insert the following line:
* * * * * bash /home/pi/autocon.sh
That should do it :) The 1 in your script actually means 'run at 1 minute past the hour' and thus in your case, 1 minute past every hour - easy mistake! Replacing it with a * means every minute.
The syntax is:
minute - hour - day of month - month - day of week - command
Additionally, if you make your script executable, like so:
sudo chmod +x /home/pi/autocon.sh
You can omit the 'bash' command, and simply use:
* * * * * /home/pi/autocon.sh
And unless you're using the two lines at the top for something in particular, you can omit those too.
For clarity, Barmar's comment on my original post:
In per-user crontab files you don't put the username. But in
/etc/crontab you do.
To run a cronjob every minute, all the values must be stars. Your cronjob is set to run at 1 minute past the hour every hour.
It should be:
* * * * * root bash /home/pi/autocon.sh

Bash Script with Crontab

I'm trying to setup a crontab I have this in my current job in the current user I'm logged into
* * * * * /CS/day/get_info.sh
get_info.sh is supposed to output a text file every minute and I suspected that it would output a file in the same directory as the script is located but it doesn't.
I've also checked the syslogs to see if I could figure this out.
(user) CMD (/CS/day/get_info.sh)
(user) MAIL (mailed 46 bytes of output but got status 0x0001#012)
Can someone explain to me why this is happening?
Thanks
man cron tells you:
When executing commands, any output is mailed to the owner of the
crontab (or to the user named in the MAILTO environment variable in the
crontab, if such exists). The children copies of cron running these
processes have their name coerced to uppercase, as will be seen in the
syslog and ps output.
So you have to
cd into the appropriate directory yourself (cron will use $HOME)
redirect ANY output to a file of your choice
You can do both things in the crontab. But I recommend to do it in the first lines of the script itself:
#!/bin/bash
cd WHEREEVER_YOU_WANT
exec > YOUR_LOG_FILE 2&>1
The script is run in the home directory of the user and the file should be there as well. If you want it in the same directory as the script, either do a cd in your script or modify your crontab entry:
*/1 19-20 * * * cd /CS/day; /CS/day/get_info.sh
Another common problem with crontab entries is the environment. If the script works correctly in your terminal, try debugging it, when it is run from cron:
40 11 * * * bash -x /CS/day/get_info.sh >/tmp/get_info.sh.log 2>&1
Run it once only with current time, because otherwise you will overwrite your log file every minute.
On my case, I just had to install and configure an smtp client.

crontab is not running my script

I'm new to cron jobs. I read a post on how to write a cron job with crontab.
So my crontab looks like this:
1 * * * * /Users/apple/Desktop/wget/down.sh
which basically means that every minute i want to execute the script :down.sh. Now the script runs
fine manually. The script is a simple program that downloads a PDF from the internet:
#!/bin/bash
wget -U Mozilla -t 1 -nd -A pdf "http://www.fi.usj.edu.lb/images/stories/HoraireS08/3eli.pdf" -e robots=off;
I don't know why it's not running every minute once the terminal tells me that he's installing the new crontab.
Can somebody help me please?
Solution:
Thank you all for your help, the syntax as mcalex said should be
* */1 * * * path/to/script
if you want it to be executed every hour.
The cron job was working normally.However my mistake was simply writing permissions, in fact while executing the wget command, it's supposed to write the pdf file in the current workind directory which is a system directory in case of the cron tab. so i solved my problem simply by navigating to the Desktop directory before executing the wget command like so:
cd /Users/apple/Desktop/wget
and then do whatever i want to do.
PS: i should include the full path of the wget command too.
Thank you all for you help again:)
When you put 1 in the first column, it will run on the first minute (of every hour). In order to get it to run in every minute of every hour, you need to set the minute column as */1
So your line should read:
*/1 * * * * /Users/apple/Desktop/wget/down.sh
supporting links:
job every minute: https://bbs.archlinux.org/viewtopic.php?id=59180
job every 5 minutes: http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/
1 * * * * /Users/apple/Destop/wget/down.sh
From this entry script will never run on every minute because it will run on first minute of every hour.
Make this change to your crontab file to run this script every min.
"* * * * * /Users/apple/Destop/wget/down.sh"
Do you have a typo? It looks like you might have mis-typed Desktop?
Another thing to do is to redirect the output of running the script to a file so you can see what's going on like this:
1 * * * * /Users/apple/Destop/wget/down.sh >> /tmp/cron.out
and then check out the file to see what's going on.
Did cron send you some email detailing what went wrong?
Does the script under that path exist?
Note that cron uses /bin/sh to execute commands.
Did you set a proper PATH in your script? wget may not be in the default PATH or there may be no PATH at all. Try using /path/to/wget in the script.
Note that downloading the same PDF file once a minute is probably a silly idea, though...
If your cronjob is writing things to disk then be careful that system preference "Put hard disk to sleep when possible" is unchecked.
This was preventing my cronjob backup tasks to execute.

Resources