In Linux, is there a way to run a cron job in the foreground (or interactive mode)? (I have a program that runs periodically to accept user input and do some processing. So I want to schedule it as a cron job that can run in the foreground).
When running cron as your ENTRYPOINT in a container, make sure you start cron in foreground mode ( cron -f ) and consider redirecting your job's stdout and stderr to crons (eg. */1 * * * * root /app1/test.sh > /proc/1/fd/1 2>/proc/1/fd/2 ).
Cron is a daemon, a long-running process that only needs to be started once, and will run constantly in the background. Cron wakes up every minute, examines its list of things to do to see if any scheduled tasks need to be executed, and if so it executes them.
What does * mean in Cron? The asterisk * is used as a wildcard in Cron. * sets the execution of a task to any minute, hour, day, weekday, or month.
If you don't have a GUI and you only have the terminal, divert the exit to tty. Execute ´tty´ and it will return the device to which you will redirect the output. For example, in Centos it will be something like:
/ dev / pts / 0
Then in crontab -e
you write:
1 * * * * user sh / PATH / TO / SCRIPT> / dev / pts / 0
Adjust the time in crontab according to your needs. It will only run if there is someone with that terminal open.
BUT WHAT PEOPLE ARE LOOKING FOR BY THE TITLE OF THE QUESTION:
Linux: Run cron job in foreground
The answer is nohup command_to_run &
:
1 * * * * nohup user sh / PATH / TO / SCRIPT &
nohup
allows executing a script as if it were an open terminal and solves the problem of executing the crontab. I mean when we create a script, for example:
#! / bin / bash
echo "I make it up"
And we wait for the output of the echo to do something with it. Example:
echo "I make it up"
if [[$? -gt 0]]
then
do something with the output of echo
The echo
execution response is obtained through stdout
in the tty terminal. But from crontab "there is no tty" and a crash occurs and crontab does not execute the application. This is solved with nohup
. More information: man nohup
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With