nerdexam
Red_Hat

RH302 · Question #169

Configure a cron job to display 'Hello World' on terminal 8 every two seconds.

The provided answer cat >schedule is insufficient for creating a cron job. Standard cron jobs have a minimum resolution of one minute. To achieve "every two seconds", a different approach is required, typically a loop within a script or a systemd timer. If a cron job must be…

Automation

Question

Configure a cron job to display 'Hello World' on terminal 8 every two seconds.

Explanation

The provided answer cat >schedule is insufficient for creating a cron job.

Standard cron jobs have a minimum resolution of one minute.

To achieve "every two seconds", a different approach is required,

typically a loop within a script or a systemd timer.

If a cron job must be used and "every two seconds" is interpreted as "as frequently as possible" (every minute),

and assuming a script sends output to /dev/pts/8 (terminal 8):

1. Create a script, e.g., /usr/local/bin/hello_world_script.sh

#!/bin/bash

echo "Hello World" > /dev/pts/8 2>&1

sudo chmod +x /usr/local/bin/hello_world_script.sh

2. Add a cron entry (runs every minute):

sudo crontab -e

* * * * * /usr/local/bin/hello_world_script.sh

To literally achieve "every two seconds" (not possible with standard cron directly):

One common workaround is to have a cron job run every minute, and the script itself loops:

1. Create a script, e.g., /usr/local/bin/hello_world_loop.sh

#!/bin/bash

for i in $(seq 1 30); do # 30 iterations to cover 60 seconds

echo "Hello World" > /dev/pts/8 2>&1

sleep 2

done

sudo chmod +x /usr/local/bin/hello_world_loop.sh

2. Add a cron entry to run this looping script every minute:

sudo crontab -e

* * * * * /usr/local/bin/hello_world_loop.sh

The verbatim answer from the source:

1. cat >schedule

Topics

#cron#job scheduling#shell scripting#terminal output

Community Discussion

No community discussion yet for this question.

Full RH302 Practice