Linux: Run cron job in foreground

前端 未结 4 518
眼角桃花
眼角桃花 2020-12-30 09:59

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

相关标签:
4条回答
  • 2020-12-30 10:31

    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

    0 讨论(0)
  • 2020-12-30 10:38

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

    crontab -e
    

    Then in crontab :

    0 7 * * * DISPLAY=:0 /PATH/TO/SCRIPT
    
    0 讨论(0)
  • 2020-12-30 10:43

    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.

    0 讨论(0)
  • 2020-12-30 10:48

    Assuming you are running X, you could always have it open a window on a selected display.

    0 讨论(0)
提交回复
热议问题