how to change directory and spin off process

0

0

I know how to change directory:

cd path

I know how to detach a process:

command &, nohup command > /tmp/test_output.txt 2>&1 &, nohup command > /dev/null 2>&1 &, disown command &

here's a good reference for that: How do I detach a process from Terminal, entirely?

But I don't know how to do them both at the same time, this seems to be impossible. Can anyone help me? Here are some things I have tried:

path/command
nohup cd path && command > /tmp/test_output.txt 2>&1 &
cd path && nohup command > /tmp/test_output.txt 2>&1 &
cd path && disown command &
(cd path && disown command &)
(cd path && command) &
cd path ; command &

I can get it to error, I can get it to go to that path and run (but not detached from my terminal) or I can get it to run the command detached from my terminal but at my current working directory, not the cd path.

I can't get it to detach the process from my terminal and run the command from a different directory than my own at the same time.

Can you help me with this? Is it even possible?

Legit Stack

Posted 2018-09-25T23:42:11.933

Reputation: 115

Answers

2

There are differences between nohup, disown and & (you may also find this answer interesting).

The syntax disown command you used is invalid, at least in Bash; see help disown. In some cases command & disown is enough, but

[command] still is connected to the terminal, so if the terminal is destroyed [...], the program will fail as soon as it tries to read from standard input or write to standard output.

So nohup command & seems better, however

nohup does not remove the process from the shell's job control

This seems quite harmless, but if it bothers you, just disown the job:

nohup command &
disown

But if the job terminates before disown acts then you will disown another job (if any). The right way to do this in Bash is to use $!:

$! Expands to the process ID of the job most recently placed into the background

So this is better:

nohup command &
disown $!

(Since disown after nohup is mostly cosmetic, I will omit it from now on. Use it if you need it).

To cd before command without affecting the current shell, the best way is to do it in a subshell (you tried it, but not with nohup):

(cd path && nohup command) &

or even

(cd path && exec nohup command) &

where exec makes nohup replace the subshell process that is no longer needed; nohup will then be replaced by command because this is how the tool works. This way you end up with one process instead of many.

Alternatively you can nohup an entire shell. This allows you to execute multiple commands:

nohup sh -c 'cd path || exit; command1; command2' &

(In this case exec makes sense only before command2).

Note nohup creates a file nohup.out, unless you redirect its output (see man 1 nohup). Where the file is created depends on the current working directory of the nohup process, so in general it makes a difference whether you cd before nohup (cd path && nohup ...) or the other way around (nohup sh -c 'cd path ... ').

Kamil Maciorowski

Posted 2018-09-25T23:42:11.933

Reputation: 38 429