how to convert a running process into a nohup and keep a logfile

9,993

Disowning the process and redirecting STDOUT from an already backgrounded process isn't feasible for most users. This SO answer covers the topic pretty well.

Disassociating the process from the current shell isn't so hard: you're looking for disown. From the manpage:

SIGNALS

....

       The shell exits by default upon receipt of a SIGHUP.   Before  exiting,
       an  interactive  shell  resends  the  SIGHUP  to  all  jobs, running or
       stopped.  Stopped jobs are sent SIGCONT to ensure that they receive the
       SIGHUP.   To  prevent the shell from sending the signal to a particular
       job, it should be removed from the jobs table with the  disown  builtin
       (see  SHELL  BUILTIN  COMMANDS  below)  or marked to not receive SIGHUP
       using disown -h.

....

SHELL BUILTIN COMMANDS

....

       disown [-ar] [-h] [jobspec ...]
              Without options, each jobspec  is  removed  from  the  table  of
              active  jobs.   If jobspec is not present, and neither -a nor -r
              is supplied, the shell's notion of the current job is used.   If
              the -h option is given, each jobspec is not removed from the ta‐
              ble, but is marked so that SIGHUP is not sent to the job if  the
              shell  receives a SIGHUP.  If no jobspec is present, and neither
              the -a nor the -r option is supplied, the current job  is  used.
              If no jobspec is supplied, the -a option means to remove or mark
              all jobs; the -r option without  a  jobspec  argument  restricts
              operation  to running jobs.  The return value is 0 unless a job‐
              spec does not specify a valid job.

If you want to learn more about what nohup and disown actually do, you can check out this question. (disclaimer: shameless self-promotion)

Share:
9,993

Related videos on Youtube

iamauser
Author by

iamauser

Updated on September 18, 2022

Comments

  • iamauser
    iamauser over 1 year

    I have a process currently occupying a terminal

    ]$ command some_argument
    

    I want to exit from the terminal and go home, but in the mean time I don't want to kill this running process.

    For the above running process, I want to achieve something like the following:

    ]$ nohup command some_argument >& logfile &
    
    • Andrew B
      Andrew B over 10 years
      I started to flag this question as a dupe of this, but the other question doesn't cover the topic of redirecting STDOUT after a prior backgrounding.
    • iamauser
      iamauser over 10 years
      How does disown keeps a log file that this process may produce to stdout ? Please remove the duplicate that you have flagged as disown alone doesn't answer this question.
    • Andrew B
      Andrew B over 10 years
      I updated my question a moment ago. The short version: not really feasible, unfortunately. :( (also, I had already retracted the dupeflagging+comment before your comment hit the website)
  • Jonathon Reinhart
    Jonathon Reinhart over 7 years
    The question clearly asked about an already running process.
  • TheFiddlerWins
    TheFiddlerWins over 7 years
    I clearly didn't read it then. Try disown for already running processes