Linux background running
In Linux, sometimes you need to run the script (test. sh) and executable program (exe) in the background. Use the following method:
Nohup./test. sh &
Nohup./exe &
In this way, the program can be completely run in the background. Why? If your script or executable program contains echo, the cout Command sends content to the standard output device and runs normally in the background:
./Test. sh &
./Exe &
It cannot meet the requirements. When the command is output to the standard output device, and the current shell window is closed, the command cannot find the standard output device, and the program will exit. This is certainly not the background operation you want. However, after nohup is added, nohup will be created in the current directory. out text file. When the content needs to be transmitted to the standard output device, it will be redirected to this text file, and the program will be able to run in the background.
The following scripts and C ++ code can be used to test the above ideas:
Shell script test. sh
#!/bin/bashwhile((1))do echo "Hello" sleep 1done
C ++ code: prints a line of content to the standard output every second
#include <iostream> #include "ace/Log_Msg.h" #include "ace/Event_Handler.h" #include "ace/Reactor.h" using namespace 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(interval)); } 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 handle_timeout"<<++i<<endl; } int main(int, char *[]) { My_Timer_Handler temp_timer(0,1); while(true) { ACE_Reactor::instance()->handle_events(); } };