简体   繁体   中英

Linux: Run cron job in foreground

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

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.

For GUI scripts in cron , try the following line in a shell :

crontab -e

Then in crontab :

0 7 * * * DISPLAY=:0 /PATH/TO/SCRIPT

假设您正在运行X,您可以随时在所选显示器上打开一个窗口。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM