Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 18
Like Tree8Likes
Hello, when I call a script by using "nohup script &", it runs in the background. However, this script is still a child process of another process, the shell. Right? ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Posts
    29

    Question about child processes


    Hello,
    when I call a script by using "nohup script &", it runs in the background. However, this script is still a child process of another process, the shell. Right?
    Imagine I have multiple background shell scripts running, but I want to send a kill signal to a specific background script. Is that possible? If so, how could it work?

  2. #2
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    222
    hi,

    the jobs builtin returns information about process in background.
    if your script is the second on the list printed, then
    Code:
    kill %2
    pinhead likes this.

  3. #3
    Just Joined!
    Join Date
    Oct 2012
    Posts
    29
    That's not robust enough, because it won't survice any restarts, PID is also volatile. I am looking for a mechanism that allows me to kill a background script like a normal process.

  4. #4
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    222
    then create your own scriptname.run.pid file, which will contain only the pid of the script, at the start of the script so you'll be able to get its pid by searching scriptname.run.pid, and printing its content.

    initscripts do this.
    pinhead likes this.

  5. #5
    Just Joined!
    Join Date
    Oct 2012
    Posts
    29
    Thanks. You mean something like $$ > pid_file?
    So a background process has an individual ID, but the PPID of every background prozess is 1 (the init process), because the former parent process has already been terminated?

  6. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    222
    yes,
    Code:
    echo $$ > pid_file
    and no, background processes keep their PID.
    when the terminal is closed, then they're attached to init, and get PPID 1
    pinhead likes this.

  7. #7
    Just Joined!
    Join Date
    Oct 2012
    Posts
    29
    And that's where the problem lies. When I start a nohup process and write his pid into a file, then I can't use this file afterwards to kill the nohup process.

  8. #8
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    222
    why not?
    you kill processes by their PID ! (their Parent's PID is useless)
    ?
    pinhead likes this.

  9. #9
    Just Joined!
    Join Date
    Oct 2012
    Posts
    29
    When I save the pid in a file and then call kill with the saved pid value as parameter, I get this message:
    Code:
    bash: kill: (11447) - No such process

  10. #10
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    222
    what does print
    Code:
    ps -C yourScriptName -o pid,ppid
    pinhead likes this.

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •