Subshells in Bash, using crontab - linux

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

Related

Execute a shell script everyday at specific time [duplicate]

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

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"

How to schedule cron job from shell script

I want to schedule a cron job in Linux by running a shell script.
The scenario is, I'm taking the time in HH:MM format in the shell script from the user and want to schedule a cron job from the shell script. I even want the cron job to be executed only once.
Thanks in advance...
For the crontab, you can do something like this:
cur=$(crontab -l)
new="$mm $hh * * * your_command"
echo "$cur$new" | crontab -
For one-shot, crontab is not the good candidate. Use at (note: your_command must be a file, e.g., a bash script)
at -f your_command $hh:$mm

Rescheduling a cron job remotely using a web app

I have a few cron jobs at server machine scheduled to do few things. I want to let user modify those cron jobs using a web application (jsp page). So lets say (example scenario) some job is scheduled to run at 2 PM, I want user the option to change its time.
It looked very trivial to start with, but now I am stuck. I am new to Linux. How to create cron jobs currently is : crontab - e ; and then I manually add new jobs as required. But I want to provide this capability to a remote user through a web interface.
Please help !
Let's say that your crontab has a following line:
20 3 * * * /home/somebody/somescript.sh
You can list your crontab using the following command:
crontab -l
Then you can change the scheduled time using the command sed:
sed 's/20 3 \* \* \* \/home\/somebody\/somescript.sh/30 4 \* \* \* \/home\/somebody\/somescript.sh/'
Finaly, you'll commit it to crontab again by passing the new file to the crontab command.
The result will be combination of the three commands discussed above and will be connected by pipes:
crontab -l | sed 's/20 3 \* \* \* \/home\/somebody\/somescript.sh/30 4 \* \* \* \/home\/somebody\/somescript.sh/' | crontab
The first command will list the current crontab to standard output. Then sed will replace the time and pass the replaced file to the crontab command, which will install it as a new crontab.
Be aware though, that most servlets do not have sufficient system rights to create or modify crontab.

Linux cronjob doesn't work (execute script)

I created a cronjob with the command crontab -e:
*/1 * * * * /var/lib/tomcat/webapps/ROOT/WEB-INF/scripts/test.sh
This file test.sh should be executed every minute. But it doesn't work.
If I run the script manually it works fine. So I think the problem is the cronjob not the script ;)
Are there any permissions or something else which block the cronjob?
Is the cronjob syntax correct?
Thx
For a start, you don't need the /1 if you want it done every minute. Just setting the minute field to * will do.
Next, you should place, as the first lines in your test script (though after the #! line if it's there):
env >/tmp/test.sh.dummy
set >>/tmp/test.sh.dummy
and see if that file shows up.
That will tell you if the script is running or not.
If it's not running, check to make sure cron itself is running:
pax> ps -ef | grep cron | grep -v grep
root 1048 1 0 08:45 ? 00:00:00 cron
(mine is).
If it is running, the most likely problem is that the environment under which cron runs your jobs is nowhere near the environment your shell gives you. Examine the differences between what was output to your /tmp/test.sh.dummy file and what your shell gives your when you execute env ; set.

Resources