Linux: Run Cron Job in Foreground

Linux: Run cron job in foreground

Try this out on your user's crontab:

@hourly DISPLAY=:0 xterm -e /path/to/my/script.sh

It will open (hourly) an xterm with your script executing, and exit after your script exits. Of course, you should modify the @hourly part to suit your needs.

Use linux command output in crontab

If you start a job on your console and background it you can then bring it to foreground. If the task is not yours or not started on your terminal then you can not.

How to run a cron job inside a docker container?

You can copy your crontab into an image, in order for the container launched from said image to run the job.

See "Run a cron job with Docker" from Julien Boulay in his Ekito/docker-cron:

Let’s create a new file called "hello-cron" to describe our job.

# must be ended with a new line "LF" (Unix) and not "CRLF" (Windows)
* * * * * 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.

If you are wondering what is 2>&1, Ayman Hourieh explains.

The following Dockerfile describes all the steps to build your image

FROM ubuntu:latest
MAINTAINER docker@ekito.fr

RUN apt-get update && apt-get -y install cron

# Copy hello-cron file to the cron.d directory
COPY hello-cron /etc/cron.d/hello-cron

# Give execution rights on the cron job
RUN chmod 0644 /etc/cron.d/hello-cron

# Apply cron job
RUN crontab /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

(see Gaafar's comment and How do I make apt-get install less noisy?:

apt-get -y install -qq --force-yes cron can work too)

As noted by Nathan Lloyd in the comments:

Quick note about a gotcha:

If you're adding a script file and telling cron to run it, remember to

RUN chmod 0744 /the_script

Cron fails silently if you forget.


OR, make sure your job itself redirect directly to stdout/stderr instead of a log file, as described in hugoShaka's answer:

 * * * * * root echo hello > /proc/1/fd/1 2>/proc/1/fd/2

Replace the last Dockerfile line with

CMD ["cron", "-f"]

See also (about cron -f, which is to say cron "foreground") "docker ubuntu cron -f is not working"


Build and run it:

sudo docker build --rm -t ekito/cron-example .
sudo docker run -t -i ekito/cron-example

Be patient, wait for 2 minutes and your commandline should display:

Hello world
Hello world

Eric adds in the comments:

Do note that tail may not display the correct file if it is created during image build.

If that is the case, you need to create or touch the file during container runtime in order for tail to pick up the correct file.

See "Output of tail -f at the end of a docker CMD is not showing".


See more in "Running Cron in Docker" (Apr. 2021) from Jason Kulatunga, as he commented below

See Jason's image AnalogJ/docker-cron based on:

  • Dockerfile installing cronie/crond, depending on distribution.

  • an entrypoint initializing /etc/environment and then calling

    cron -f -l 2

Run two commands in cron

Using the full path is defintely better then first using cd. To get the result of the cronjob, you could just output to file like this:

59 * * * * /home/sansal/Scripts/usbreset /dev/bus/usb/002/003 &>> /home/sansal/usbreset.log


Related Topics



Leave a reply



Submit