Get the Pid of the background process executed using bash -c

Bidyut

I need to execute a program in background, and the program should keep on running, after the shell execute. I am executing the script using nohup and using bash -c to make it run in background.

#!/bin/bash
cd /mnt/go/src/github.com/something && pwd && bash -c  "nohup go run apps/something/main.go >> /var/logs/go/crash.log 2>&1 &"

I need to get the pid of the nohup command executed in the spawned shell using bash -c, I tried using

 cd /mnt/go/src/github.com/something && pwd && bash -c  "nohup go run 
apps/something/main.go >> /var/logs/go/crash.log 2>&1 & echo $! > /var/run/something.pid"

But the output is empty string, is there any better way to solve this issue. Because of using bash -c I cannot get the PID using $?.

Bayou

Why are you chaining everything with &&? If you want to stop the script if an error occurs, just add set -e below the shebang. It makes sure that the script stops executing if a command returns an exit status not equal to 0 . Don't use set -e (read link in comments).

As I've wrongly commented in a previous edit, it seems that your code should be valid in the first place (thx @tripleee).

I think the error occurs because you use " instead of '. The error occurs because Bash first has to parse the string to replace variables. Since the current shell hasn't created a child process, $! is empty and therefore your file as well. Try it with an single quote:

#! /bin/bash
cd /mnt/go/src/github.com/something
pwd
bash -c  'nohup go run apps/something/main.go >> /var/logs/go/crash.log 2>&1 & echo $! > /var/run/something.pid'
cat /var/run/something.pid

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Get return value of process by PID

分類Dev

Conditionally run process in background with bash

分類Dev

When does the start-process get executed?

分類Dev

Bash: receive messages from background process

分類Dev

Bash: receive messages from background process

分類Dev

How to run process in background using gcloud ssh

分類Dev

BASH: Run script.sh in background and output that instance's pid to file?

分類Dev

If I know the PID number of a process, how can I get its name?

分類Dev

How to get parent PID of a given process in GNU/Linux from command line?

分類Dev

Bash the function is executed twice

分類Dev

Which process has PID 0?

分類Dev

Background tasks are being queued and not executed

分類Dev

Can't get the background color using DOM

分類Dev

Get PID of the inserted row

分類Dev

Batch Get specific PID

分類Dev

Is the PID of a child process always greater than the PID of its parent on Linux?

分類Dev

bash shutdown hook; or, kill all background processes when main process is killed

分類Dev

history/log of command-lines executed to launch processes(pid)

分類Dev

Android threading, background process

分類Dev

"Variabilize" the ampersand (background a process)

分類Dev

Notification Manager on background process

分類Dev

Find the PID of a process that uses a port on Windows

分類Dev

How to get the content of a function in a string using bash?

分類Dev

Using preRemove/postRemove events to get which queries can be executed and which can't

分類Dev

How to get a current class name and methods that executed in the class file using testNg listeners?

分類Dev

Get PID of system call in Ruby

分類Dev

Get executed sql statement in pymysql

分類Dev

how to get executed a query of two tables of same columns to datagridview columns only once in c# and Sql

分類Dev

Get background image from webpage using DOM XPATH

Related 関連記事

  1. 1

    Get return value of process by PID

  2. 2

    Conditionally run process in background with bash

  3. 3

    When does the start-process get executed?

  4. 4

    Bash: receive messages from background process

  5. 5

    Bash: receive messages from background process

  6. 6

    How to run process in background using gcloud ssh

  7. 7

    BASH: Run script.sh in background and output that instance's pid to file?

  8. 8

    If I know the PID number of a process, how can I get its name?

  9. 9

    How to get parent PID of a given process in GNU/Linux from command line?

  10. 10

    Bash the function is executed twice

  11. 11

    Which process has PID 0?

  12. 12

    Background tasks are being queued and not executed

  13. 13

    Can't get the background color using DOM

  14. 14

    Get PID of the inserted row

  15. 15

    Batch Get specific PID

  16. 16

    Is the PID of a child process always greater than the PID of its parent on Linux?

  17. 17

    bash shutdown hook; or, kill all background processes when main process is killed

  18. 18

    history/log of command-lines executed to launch processes(pid)

  19. 19

    Android threading, background process

  20. 20

    "Variabilize" the ampersand (background a process)

  21. 21

    Notification Manager on background process

  22. 22

    Find the PID of a process that uses a port on Windows

  23. 23

    How to get the content of a function in a string using bash?

  24. 24

    Using preRemove/postRemove events to get which queries can be executed and which can't

  25. 25

    How to get a current class name and methods that executed in the class file using testNg listeners?

  26. 26

    Get PID of system call in Ruby

  27. 27

    Get executed sql statement in pymysql

  28. 28

    how to get executed a query of two tables of same columns to datagridview columns only once in c# and Sql

  29. 29

    Get background image from webpage using DOM XPATH

ホットタグ

アーカイブ