Execute a shell script everyday at specific time [duplicate] - linux

This question already has answers here:
How to write a cron that will run a script every day at midnight?
(6 answers)
Closed 7 years ago.
I have a simple shell script that just checks the contents of a directory and if anything was added during the day makes a copy of it to a backup folder.
I'd like to execute this script at the end of each day (let's assume at 23:55).
The system(Debian) which this scripts reside on it, is always on (kind of server)
How can I do that?

To add a crontab job, type the following command at a UNIX/Linux shell prompt:
$ sudo crontab -e
Add the following line:
1 2 3 4 5 /path/to/script
where
1: Minutes (0-59)
2: Hours (0-23)
3: Days (1-31)
4: Month (1-12)
5: Day of the week(1-7)
/path/to/script - your own shell script
In your case it would be:
55 23 * * * /path/to/yourShellScript

You want to edit your crontab file using
crontab -e
Then you want to add
55 23 * * * COMMAND TO BE EXECUTED
for more info look at this

I'm anything, but a linux expert, but a quick Google search conjured up this:
watch -n <your time> <your command/script>
This should do the trick. For more information, check this out: http://www.linfo.org/watch.html

sudo crontab -e
55 23 * * * some_shell_script.sh

Check out the Cron task scheduler built in to Debian. Simply add an entry for your script to your crontab file (see: https://help.ubuntu.com/community/CronHowto).

Related

Subshells in Bash, using crontab

I am currently working on some school project where we should be dealing with cron jobs, basically, we are building a simple CLI to do CRUD operations using Bash.
I found this snippet of code that inserts a new job into my crontab, yet I have no idea how it works... I understand it uses subshell and pipes, yet I just don't know why I would have to do it
read job
{ crontab -l; echo "$job"; } | crontab -
crontab is a file which contains jobs (instructions) for cron daemon (time-based job scheduler for Unix operating system).
If you put your bash lines into a script , let's say cron_test.sh
#!/usr/bin/sh
read job
{ crontab -l; echo "$job"; } | crontab -
And afterwards if you execute the script ./cron_test.sh, you'll see that the scripts awaits from stdin your input (which stores it into variable named job) in order to create a new job for your user.
Be careful because you have to respect the job syntax:
1 2 3 4 5 /path/to/command arg1 arg2
where:
1: Minute (0-59)
2: Hours (0-23)
3: Day (0-31)
4: Month (0-12 [12 == December])
5: Day of the week(0-7 [7 or 0 == sunday])
/path/to/command – Script or command name to schedule
Some commands:
crontab -l - list current crontab (for the user which you're using)
crontab -e - edit the crontab file

Cron run every minute ( Runs in bash but not in cron)

This have been discussed several times in previous post. I followed given advise but it does not work for me. I have two scripts which are run by the cron service every minute. To my surprise, only one runs per minute( 1st in the list below), the other fails (2nd in list below). Most surprising, when run direct from the terminal, both scripts execute fine.
Cron setup :
*/1 * * * * /home/user/Desktop/scripts/generatepattern.sh
*/1 * * * * /home/user/Desktop/scripts/getnextfile.sh
File permissions are:
-rwxr--r-- 1 user user 522 Jul 25 16:18 generatepattern.sh
-rwxr--r-- 1 user user 312 Jul 25 23:02 getnextfile.sh
The code for the non-schedulable( not running in cron ) is :
#!/bin/bash
#Generate a file to be used for the search
cd /home/user/Desktop/scripts
no=`cat filecount.txt`
if test $no -lt 20
then
#echo "echo less"
#echo $no
expr `cat filecount.txt` + 1 >filecount.txt
fi
In the last line you wrote cat filecount.txt instead of cat /home/user/Desktop/scripts/filecount.txt
I discovered the main issue was that the new cron settings only get used when the vi editor gets closed. Changes have to be made on the editor and a :wq command issued so that the new settings get installed. Just issuing :w command does not work since no install happens(this was my mistake). I realised this after issuing :wq command on vi and the following output displayed :-
# crontab -e
crontab: installing new crontab
Thanks to all other suggestions made.

Linux bash shell script output is different from cronjob vs manually running the script

I wrote a linux bash shell script which works fine except the output when I run it manually is different than when I run it from a cronjob.
The particular command is lftp:
lftp -e "lcd $outgoingpathlocal;mput -O $incomingpathremote *.CSV;exit" -u $FTPUSERNAME,$FTPPASSWORD $FTPSERVER >> ${SCRIPTLOGFILE} 2>&1
When I run the script manually, the ${SCRIPTLOGFILE} contains a lot of info such as how many files/bytes/etc transferred. But when I run the same script from a cronjob there is no output unless there was an error (such as could not connect). I have tried various terminal output configurations but none work for this lftp command. Suggestions?
It's worth reading this:
crontab PATH and USER
In particular, cron won't set the same environment variables you're used to an interactive shell.
You might want to wrap your entire cron job up in a script, and then you can, for example, temporarily add some code like export >> scriptenvironment.txt and see what the difference is between the cron invoked script and the interactively invoked script.
Try man 5 crontab for details.
Once you know what envrionment variables you need for your script to run, you can set them in the crontab as necessary, or source at the start of your own script.
EXAMPLE CRON FILE
# use /bin/sh to run commands, overriding the default set by cron
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"

Why my scheduled job is not working?

I define a job with crontab like this
0 2 * * * dbadmin . /home/dbadmin/back.sh
it is not root I want to run this .sh file with dbadmin user.
but when I checked it is not working.
in the log it gives this:
Feb 22 21:16:01 localhost crond[14634]: (*system*) BAD FILE MODE (/etc/crontab)
Feb 22 21:16:01 localhost crond[14634]: (dbadmin) RELOAD (cron/dbadmin)
Feb 22 21:16:01 localhost crond[28451]: (dbadmin) CMD (dbadmin . /home/dbadmin/back.sh)
How can I fix this? thanks in advance
Make a crontab entry as dbadmin without the username in it:
0 2 * * * /home/dbadmin/movefolder.sh > /home/dbadmin/cron.out 2>#1
Each day the logfile /home/dbadmin/cron.out should be replaced by a new one.
When you are confident about the cron+movefolder, replace the outputfile with /dev/null.
When above fails, check calling the script as dbadmin:
sh /home/dbadmin/movefolder.sh
When this one works and cron fails, it might be the environment. Try saomething like
0 2 * * * . /home/dbadmin/.profile; /home/dbadmin/movefolder.sh > /home/dbadmin/cron.out 2>#1
Using the crontab program, you normally have access only to the 5 scheduling fields (minute, hour, day of month, month and day of week). However, with Vixie cron (usually on Linux) by editing the system crontab file (/etc/crontab, as well as files in /etc/cron.d) you can use the 6th field for the username. For example, see How to specify in crontab by what user to run script?
If you use crontab to enter this line
0 2 * * * dbadmin . /home/dbadmin/back.sh
^^^^^^^
the "dbadmin" username is treated as the command to execute. You can (as noted in crontab's manual page) use that line in /etc/crontab. I pointed out that this is Vixie (also known as ISC) crontab. Legacy systems such as Solaris have a less capable crontab which would not allow specifying the user to run under.
According to cron's manual page, it will send output via email. Perhaps there was no email because the command "dbadmin" failed.
You need to use sh command for executing sh file.like following
0 2 * * * dbadmin sh /home/dbadmin/movefolder.sh
Hope it works.
Thank you.

linux script to run multiple commands at specific time

I need some help in writing a Linux script that do the following:
command 1
command 2
wait 10 minutes
command 3
command 4
and this script should run automatically at specific time for example 4 am...
Thank in advance
You can create a script.sh like:
#!/bin/bash
command 1
command 2
sleep 600 # 600 seconds = 10 min
command 3
command 4
And then create a cronjob:
0 4 * * * /bin/bash /path/to/script.sh
You can see more info of cron in https://stackoverflow.com/tags/cron/info
if you want the job to run once at a future time, instead of cron use at
at 4am tomorrow <<END
command 1
command 2
sleep 600
command 3
command 4
END
One of the advantages of at is that it will execute the commands using your current environment. The limited environment provided by cron is a cause of confusion for many people.

Resources