Linux background running

Source: Internet
Author: User

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();      }  };  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.