Run script in background?

2019-02-20 00:04发布

问题:

Simple question: Is there a way to run a script in the background with out terminal running?

More detail and background: I had an app that read an apps .log file and puled information from it, then provide information and statistics from the information in the log. An update to the app changed the way the .log file was written and delete information and duplicates the log in a manner that i have been unable to predict.

the app that was designed to interface with the log was not coded to check for such changes. so when it attempts to gather information after the log change it stops working.

A "hack" has been devised to run a tail -f, then hexed the app to point at the new file. (The "hack" works) I would like to run the tail in the background so that the user doesn't interrupt it... breaking it...

-sorry for the (possibly) longer than needed description. BUt i figured a more detailed question would get me a precise answer.

Thanks in advance!

~¥oseph

回答1:

The answer depends on if you need to be able to re-connect to the process after exiting the shell. If the process is non-interactive and can simply be left alone, then "nohup program &" should do the trick. But that won't let you continue to interact with the program after you've closed the shell.

If it's a interactive program, then your best bet is to use screen or one of the other terminal-multiplexers. You start "screen" which gives you a new shell, in this you start whatever program you want, the usual way, say "nano myfile.txt".

When you want to close the shell, but leave the program running, you press C-a d ('Detach') to detach from screen. it keeps running, but in the background, and will keep running even if you log out.

When you then later want to reconnect to screen you open a new shell and type "screen -r" (reconnect), this leaves you right where you where.

Screen also lets you run several different shells in a single terminal-window and is a neat tool overall. Check it out.