I have a server running Linux operating system. I am trying to schedule a cron job, in crontab file, to run a task every two weeks (Fortnight) on Tuesday at 9 am. I tried to run the following command:
0 9 * * 2 root test $((10#$(date +\%V)\%2)) -eq 0 && ( java -jar /email/emailRemind.jar )
This script does not work, it shows this message
/bin/sh: 1: arithmetic expression: expecting EOF: 10#24%2"
Any thoughts?
Related
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
I have a server running Linux operating system. I am trying to schedule a cron job to run a task every two weeks (Fortnight) in Tuesday at 9am.
I can only manage to run the task manually by comment (if I don’t want to run the job for this week) and uncomment (if I want to run the job for this week) as following:
0 9 * * 2 root java -jar test.jar // will run
# 0 9 * * 2 root java -jar test.jar // will not run
I have attempted to use the following cron job:
0 9 * * 2 case $(($(date +\%s) / (60*60*24*7))) in *[02468]) root java -jar test.jar
But this cron script does not seem to work.
Any thought
I would try to execute at 4AM every one of two tuesdays :
0 4 * * 2 test $((10#$(date +\%W)\%2)) -eq 1 && execute_cmd
We first get week number with date and correct formatting, then the 'one time out of two' thing is done by using 'test' command.
'test' evaluates to true or false depending on a given expression, here, it evaluates whether $((10#$(date +\%W)\%2)) equals 1 (hence -eq 1), in other words it returns true if the week number is odd.
So this crontab will execute on odd weeks, on tuesday (2) at 4 AM (0 4).
More in-depth details here :
https://serverfault.com/questions/633264/cronjob-run-every-two-weeks-on-saturday-starting-on-this-saturday
As part of the setup of a docker container, the following gets injected into crontab:
*/10 * * * * /opt/run.sh >> /opt/run_log.log
According to the behavior of crontab, when should the first run kick off? Should the 10 minute cycle begin instantly, or 10 minutes after this is put into crontab. Neither behavior is happening so I am trying to debug this in more depth by trying to understand the intended behavior.
This cron sandbox simulator gives you an idea:
Mins Hrs Day Mth DoW
*/10 * * * *
This run time (UTC) Sat 2016-Jan-23 0653
Forward Schedule Sat 2016-Jan-23 0700
Sat 2016-Jan-23 0710
Sat 2016-Jan-23 0720
It uses the syntax:
Every nth '0-23/n', '*/2' would be every other.
'*/1' is generally acceptable elsewhere, but is flagged here as possibly an unintended entry.
See for example "Run a cron job with Docker" (by Julien Boulay)
Let’s create a new file called “crontab” to describe our job.
* * * * * root echo "Hello world" >> /var/log/cron.log 2>&1
# An empty line is required at the end of this file for a valid cron file.
The following DockerFile describes all the steps to build your image
FROM ubuntu:latest
MAINTAINER docker#ekito.fr
# Add crontab file in the cron directory
ADD crontab /etc/cron.d/hello-cron
# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron
# Create the log file to be able to run tail
RUN touch /var/log/cron.log
# Run the command on container startup
CMD cron && tail -f /var/log/cron.log
Then you can build the image with
sudo docker build --rm -t ekito/cron-example .
And run it:
sudo docker run -t -i ekito/cron-example
Be patient, wait for 2 minutes and your commandline should display:
Hello world
Hello world
If you replaced the first '' by '/10', you would have to wait to the next 0 or 10 or 20 or... of the hour.
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"
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.