Sometimes in Linux you need to run scripts (test.sh) and run-time programs (EXE) in the background, using the following methods, for example:
Nohup./test.sh &
Nohup./exe &
So the execution of the program can be executed completely in the background, why? Because you assume that you have echo in your script or executable program. cout the instructions for conveying content to standard output devices, normal background execution:
./test.sh &
./exe &
is not able to meet the requirements when the instructions go to the standard output device output. After the current shell form is closed, the command cannot find the standard output device and the program exits. This is certainly not the background you want to perform.
However, after adding nohup, Nohup will create nohup.out text files in the current folder. When content needs to be transferred to a standard output device, it is redirected to the text file, and the program is able to actually execute the background.
The following script and C + + code can test the above view:
Shell Script test.sh
#!/bin/bashwhile ((1)) do echo "Hello" Sleep 1done
C + + code: Print a line of content to standard output every second
#include <iostream> #include "ace/log_msg.h" #include "ace/event_handler.h" #include "ace/reactor.h" using names Pace std; #include "Ace/thread_manager.h" class My_timer_handler:public Ace_event_handler {public:my_timer_handler (const int delay,const int interval); ~my_timer_handler (); virtual int handle_timeout (const ace_time_value¤t_time, const void *); Private:int i; Long ID; }; My_timer_handler::my_timer_handler (const int Delay,const int interval): I (0) {This->reactor (ace_reactor::instance ()); Id=this->reactor ()->schedule_timer (this, 0, ace_time_value (delay), Ace_time_value (Interva L)); } my_timer_handler::~my_timer_handler () {cout<< "~my_timer_handler ()" <<endl; } bool Stop_event_loop = FALSE; int my_timer_handler::handle_timeout (const ace_time_value¤t_time, const void *act = 0) {cout<< "Hello Handl E_timeout "<<++i<<endl; } int Main (int, char *[]) {My_timer_handler temp_timer (0,1); while (true) {ace_reactor::instance ()->handle_events (); } };
Linux background execution