Command Execution with Nohup in Background

command execution with nohup in background

The & at the end indicates that the command should be running in the background. That means, you can continue to enter commands into the shell.

For your particular question, it does not make any difference as it seems you want to close the terminal window right afterwards anyway.

what is the time about nohup command execute in shell?

It's because you put the nohup command in the background with &.
There is no output immediately from the command when you put it in the background as it is running in a separate shell, and your current shell immediately goes to the sleep command. By the time you return from sleep, the nohup background process has returned and outputs the value.

If you remove the & (and thus run the commands in the same shell) you will see the order changes.

How to get execution time of a specific script that is running, but in the background?

You can add execution time nohup log by execution the command on this way:

nohup /usr/bin/time php script...

Having to hit enter with nohup command in bash

It looks like you have shell debug tracing (set -x mode) turned on, meaning bash will print each command before executing it (but after expanding variables etc). Since the nohup java ... command is backgrounded, this happens asynchronously, and happens to get printed after the shell has printed its prompt, so you get "$ " (your shell prompt) followed by "+ nohup java ... (the debug trace).

(Note: you have errors & output from the command redirected to nohup.out, but since the trace is printed by the shell, not the command itself, the redirect doesn't apply.)

You don't actually need to press return at this point; the only thing pressing return does is get you a new, clean (not mixed with debug tracing) prompt. You could just enter another command as normal. However, since the shell is confused about where on the line you are, features like command editing and history recall may not work properly.

If you want to avoid this, I think your main options are to either turn off debug tracing (set +x) before running the command, or add a delay before the shell prints its next prompt:

nohup java -jar webservices-0.0.1-SNAPSHOT.jar > nohup.out 2>&1 & sleep 1

Here, the nohup java ... command runs in the background as usual, but the sleep 1 command runs in the foreground and so the shell won't print its next prompt for a second (which should be enough time for the background process to print its debug trace).

How to execute linux command in background?

nohup sleep 10 2>/dev/null &

The nohup command prints a message to stderr, and 2>/dev/null sends stderr to /dev/null.



Related Topics



Leave a reply



Submit