Confusion about Crons - linux

this is my first time writing script for cron job.
I wrote my code in shell, (which it works) and I'm trying to set it up for cron.
So here is my question. How do I set up the cron? Am I suppose to write
10 * * * * /home/workstation/deleter.sh (I want it to run every 10min)
right underneath #!/bin/sh? How would I execute it? (deleter.sh has permission via chmod)
man 1 crontab returns "No entry for crontab in section 1 of the manual"
I'm really lost and confused right now. If someone know how to set up cron please tell me!!
Thanks in advance
#!/bin/sh
counter=0
logloc=/home/ServerLogs
backup=/home/test
## Reads the location of the file systems that needs to be investigated from location.txt
## and save it into an array
while read -r line; do
Unix_Array[${counter}]=$line;
let counter=counter+1;
done < location.txt
## Reads Email recipients and save it into an array
More code continues from here......

The following will open your environment's text editor and load the crontab:
crontab -e
Your crontab entry is mostly correct. In order for your script to run every ten minutes it should be changed to:
*/10 * * * * /home/workstation/deleter.sh
The entry you indicated would run the script at the 10th minute of every hour.

To setup the cron, you can do one of two (main) things. The first would be to place the specified line in /etc/crontab. The second would be to run crontab -e and place the line in there. I would recommend to use crontab -e so the cron will execute as your own user account.
If the full path to the script is /home/workstation/deleter.sh and it does have execute-privileges, as you specified - your current line will have it execute 10-minutes past the hour, every hour. To get it to execute every 10 minutes, you'll have to use */10, like this:
*/10 * * * * /home/workstation/deleter.sh

this might help
http://www.manpagez.com/man/5/crontab/
you need to get an entry into your crontab

One of the best links I came across when I first learned about cron! Bookmark it
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/

Related

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...

Running a crontab every 15 minutes not working on linux redhat

I want to run a crontab every 15minutes. I tried this:
0 */15 * * * ./usr/My_PATH/run.sh
But I get this error:
0 command not found
Is there something wrong with the syntax ?
Many thanks.
UPDATE:
I corrected the script and I tried this:
*/15 * * * * /My_Path/run.sh
and this
0,15,30,45 * * * * /My_Path/run.sh
In both cases I get an error.
#1 bash: */15: No such file or directory
#2 bash: 0,15,30,45 command not found
If this:
0 */15 * * * ./usr/My_PATH/run.sh
fails with this error:
0 command not found
then you're trying to run it as a shell command. You need to feed it to the crontab command. There are several ways to do this.
crontab -l will list the current contents of your crontab; it doesn't modify it.
crontab -e will open (a copy of) your crontab in a text editor and let you modify it. This is probably the simplest way to update it.
crontab filename reads the specified file and replaces your current crontab with its contents. (If you already have a crontab, this will quietly clobber it.)
The method I recommend is to keep a separate file containing your crontab (say, crontab.txt).
First, if you already have a non-empty crontab (check with crontab -l), save it to the file:
crontab -l > crontab.txt
Make whatever additions or other changes you want to that file, and then use
crontab crontab.txt
to install the updated crontab.
You can keep backup copies (I maintain mine in a source control system) so you can recover if you mess something up. And you can do a quick crontab -e if you want to test something, then re-run crontab crontab.txt to revert to the stored crontab.
The syntax of the crontab line in your question:
0 */15 * * * ./usr/My_PATH/run.sh
is correct, but the path ./usr/My_PATH/run.sh looks like it may be incorrect. Cron jobs run from your home directory, so the path is valid only if the usr directory is directly under your home directory (and in that case the ./ is unnecessary). It's probably better to specify the full path, which can start with $HOME/.
Yes.
First field is minutes. Second field is hours. You're setting it off at zero minutes past the hour, every 15th hour. So basically - 15:00 each day.
You want:
*/15 * * * * /some_script
Furthermore - ./ - it's a relative path, and that's probably a bad idea with cron, because it doesn't chdir to run stuff. Use an absolute path to avoid confusion. If you absolutely need to be in a particular directory for the script to work, you can try:
cd /path/to/script && ./this_script
So it's quite possible that you've got broken permissions or just not finding a relative path that you're using.

BASH - Most efficient method of setting this script as a timed/routine event?

I've recently started a simple project, just to help me learn the BASH scripting language a little better. Anyway, this script is set to alternate/rotate the user's desktop background/wallpaper at a given interval.
Given that this task would most likely be done every 30 minutes to 1 hour, how should I go about doing this. Would 30 minute/1 hour timers be very inefficient? Or, could cronjobs do a better job for me?
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
Could you provide some sort of an idea into the syntax, if you can, as well.
This would be a suitable job for cron. cron would take care of invoking the script at regular intervals. You would not then have to be concerned in your script when the script should run and managing a script running in the background.
Running in the background would be extravagent as the script does not need to do much - not much more than change the current desktop setting. Typically the script would only take a small fraction of a second to complete the task.
cron entries have six fields-:
mins hours day month day-of-week path_to_command
0-59 0-23 1-31 1-12 0-6 command
days of the week start on Sunday. 0=Sunday, 1=Monday etc.
cron entry to run the script every hour for all days and months-:
0 * * * * /path/change_wallpaper.sh
to list your current cron jobs, type
crontab -l
Edit your cron jobs and add the new cron entry-:
crontab -e
Check the new setting is in place -:
crontab -l
I would personally run the script using following crontab:
0 * * * * $HOME/changewallpaper.sh
which you can install as a user with this command
crontab -e
Other solutions include running daemon script from file ~/.xprofile
For more information please refer to
man crontab
man 5 crontab
Also check out this project Variety.
Also, how could I get this script to run in the background, so that a terminal window is not always required to be open?
That would be a daemon. And there's no need to write your own. It's a bit tedious in bash if you want pidfile, start|stop|restart etc. Just add a new cronjob which'll execute your script every n minute or something.
Edit your cronjobs
crontab -e
Execute script every 30 min: (not the same as 30, which would do it every hh:30!)
*/30 * * * * /path/to/your/script
Restart cron. How depends on distro, here's Ubuntu:
service cron restart
List cronjobs:
crontab -l

Perl script executed by Cron failed

I maintained a database (MySQL), I would like back up some data to the database using a perl script. To save my trouble, I would like cron to do it for me, I inserted the following using crontab
*/5 * * * * blctrl /home/blctrl/code/perl/tt01.pl
However, cron never does its job, any suggestions to get it done? The Linux installed is Centos 5?
*/5 * * * * blctrl /home/blctrl/code/perl/tt01.pl
That looks like the syntax for /etc/crontab, the system-wide crontab file. The first 5 words indicate when to run the command, the 6th is the account under which to run it, and the rest of the line is the command to execute.
(The clue was that the command is under /home/blctrl, which would be the home directory for the account blctrl.)
The syntax for your own crontab, the one you feed to the crontab command, is different. You don't specify an account name, because it only runs under your own account.
Try this:
*/5 * * * * /home/blctrl/code/perl/tt01.pl
EDIT: Incidentally, the first thing I would have tried when encountering a problem like this would be to replace the command with something simple, perhaps touch /tmp/FOO. That would have told you whether the problem was with your Perl script or with your crontab.

Cron command to run PHP script every 10 minute

In my hosting i have a section for cron job like this:
(source: site-helper.com)
The PHP script is called "croned.php", which I want it to run every 10 minutes.
What I will fill in every field?
I tried but it didn't work.
Note: the full path to the script is: /home/axelzd/domains/hellodom.com/public_html/croned.php
Put */10 in the minutes whilst putting * in all other fields.
Usually you can use commas to separate the cron minutes/hours etc. - 0,10,20,30,40,50 in your minute field (but I can't guarantee your admin will take it - I know Plesk does) and * in all others . The command is more tricky, but something like this should do /usr/bin/wget -q -t 5 --delete-after URL_TO_YOUR_CRON or php PATH_TO_YOUR_PHP_FILE_ON_THE_SERVER
try this
*/10 * * * * <command_to_be_invoked>

Resources