Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
0
votes
2
answers
2146
views
Keep python script running after ssh closed
I am trying to run a script called `script.py` on a remote CentOS 7.3 box that I ssh into. I am running the script from an environment called `py`. I want the script to continue running after I disconnect. From reading other stack exchange questions I have tried conda activate py nohup python script...
I am trying to run a script called
script.py
on a remote CentOS 7.3 box that I ssh into. I am running the script from an environment called py
. I want the script to continue running after I disconnect. From reading other stack exchange questions I have tried
conda activate py
nohup python script.py &
This works, but when I disconnect the ssh the process stops running.
I have also tried
screen
conda activate py
python script.py
However, this gives me a NameError
pertaining to a function in a module that is in my py
environment but not in base
. So it looks like when I use screen
its not using the py
environment.
Finally I do not have tmux
installed and cannot do so as it is a work box.
Any other ideas?
mch56
(101 rep)
Feb 11, 2021, 11:38 AM
• Last activity: Jun 6, 2025, 03:06 AM
1
votes
1
answers
2498
views
Why does my process terminate upon log out despite nohup and disown?
I have an executable (a server made with Unity) which I want to continue to run after I log out. All the interwebs say that I should be able to accomplish this with `nohup` or `disown` or both. But it doesn't work. I do: ``` nohup /usr/bin/myexecutable & disown -h ``` and check the process list at t...
I have an executable (a server made with Unity) which I want to continue to run after I log out. All the interwebs say that I should be able to accomplish this with
nohup
or disown
or both. But it doesn't work. I do:
nohup /usr/bin/myexecutable &
disown -h
and check the process list at this point, and my process is running. Then I exit
and ssh back in, and check the process list again, and it is gone. I have tried it with and without disown
, with and without the -h
flag, etc., but nothing stops the process from disappearing when I exit the shell.
I can do it with screen
or tmux
, but I'm trying to set up a simple launch script that nontechnical people on the staff can just log in and run. Any ideas?
(I am running Ubuntu 16.04.6 LTS.)
EDIT: Someone suggested this question , but like in the comment from @mathsyouth, disown
does not work for me. @Fox, the author of the only answer to that question, says "If the program you are using still dies on shell-exit with disown, it is a different problem." So it appears this is a different problem. (And as mentioned above, his suggestion of using screen
is not helpful in my use case.)
Joe Strout
(131 rep)
Jul 7, 2020, 05:57 PM
• Last activity: May 25, 2025, 07:02 PM
5
votes
2
answers
2916
views
when to use NOHUP
I would like to know the purpose of `nohup` *these days*. I ask because I read this > Nohup is short for “No Hangups.” It’s not a command that you run by itself. Nohup is a supplemental command that tells the Linux system not to stop another command once it has started. That means it’ll keep running...
I would like to know the purpose of
nohup
*these days*.
I ask because I read this
> Nohup is short for “No Hangups.” It’s not a command that you run by itself. Nohup is a supplemental command that tells the Linux system not to stop another command once it has started. That means it’ll keep running until it’s done, even if the user that started it logs out. The syntax for nohup is simple and looks something like this:
>
> nohup sh your-script.sh &
>
> Notice the “&” at the end of the command. That moves the command to the background, freeing up the terminal that you’re working in.
>
> Nohup works with just about any command that you run in the terminal. It can be run with custom scripts as well as standard system commands and command line utilities.
With using linux distributions such as SLES 11.4, or RHEL/CentOS 7.x, I can
- remote log in to linux over the network via SSH
- run the simple program below, or any other, doing ./a.out &
- log out of linux; type exit in the putty SSH terminal
- my sample program below, or any other including bash, csh, or tcsh shell scripts, run to completion just fine without the need for nohup
- typing exit in putty to close an SSH session, I would think that qualifies as a logout?
Is there any purpose or value with nouhup
today? Is it a kernel >= 3.x thing? For however long I can remember (kernel >= 2.6) I've never used nohup
and have always just use &
with no problems.
Can someone give me a practical scenario where nohup
would be used?
#include
#include
/*
sample C code, to print numbers to a file named zz.tmp
the numbers 0 to 29 over 30 seconds
*/
int main ( int argc, char *argv[] )
{
FILE *fp;
int i;
i = 0;
fp = fopen("zz.tmp", "w" );
while ( i < 30 )
{
fprintf( fp, "%d\n", i++ );
sleep( 1 );
}
fclose( fp );
return 0;
}
**mysleep.sh**
#!/bin/bash
sleep 1000
On Centos 7.7, optiplex pc login with keyboard & mouse, my account is bash. I do nohup ./mysleep.sh
and then **control-z**. When typing jobs
I see + Stopped nohup ./mysleep.sh
A ps -ef | grep mysleep
shows that process id existing, but if I close the terminal window via X in upper right corner, that process goes away so it seems nohup
is not working?
ron
(8647 rep)
Feb 18, 2020, 02:37 PM
• Last activity: May 21, 2025, 01:38 PM
3
votes
1
answers
2827
views
rsync (but also other commands) run with nohup gets SIGHUP
I'm connecting from my computer to a HP UX B.11.31 system and am running some very lengthy there commands using nohup so I can go to sleep and do something else for a few days while they complete. Example of such a command: ``` nohup rsync --rsync-path=/usr/local/bin/rsync -e ssh /data/src usr@targe...
I'm connecting from my computer to a HP UX B.11.31 system and am running some very lengthy there commands using nohup so I can go to sleep and do something else for a few days while they complete.
Example of such a command:
nohup rsync --rsync-path=/usr/local/bin/rsync -e ssh /data/src usr@target:/data/ >> /data/log/sync.out 2>&1 &
Note: the Rsync command's target is a different HP UX server.
It'll run and run, until my computer somehow gets disconnected from the HP UX system (it's connected through SSH over a VPN over my home connection, and disconnections seem to happen once a day).
If this has happened, I can tail /data/log/sync.out
and I'll find:
Killed by signal 1.
rsync error: unexplained error (code 255) at rsync.c(549) [sender=3.0.8]
Someone suggested putting the command in a shell script and running that with nohup instead.
So I did that (with a shebang for #!/usr/bin/bash
), and then ran the command as nohup ./auto_rsync.sh > /data/log/sync.out 2>&1 &
.
The same thing happened.
The funny thing is that if I simply log out while the command is running by typing exit
, the command will keep running. So why is only my disconnect sending SIGHUPs to a process running with nohup
?
bluppfisk
(193 rep)
Oct 1, 2019, 06:22 AM
• Last activity: May 21, 2025, 10:14 AM
17
votes
3
answers
23383
views
what are practical uses of stdbuf or nohup?
I have to finish a writeup of a few coreutils commands for a course at the moment, and I can't think of a starting point for a small practical code example that demonstrates the potential uses of `stdbuf`. Has anyone used it to fix the interaction of a couple specific unix commands? I know what it d...
I have to finish a writeup of a few coreutils commands for a course at the moment, and I can't think of a starting point for a small practical code example that demonstrates the potential uses of
stdbuf
.
Has anyone used it to fix the interaction of a couple specific unix commands?
I know what it does.
It's just that the first commands that came to mind have their own buffering controls, and normal terminal output is line buffered anyway.
It must be popular for appending to logs, yet I can't find a good command to demonstrate there.
In the case of nohup
, are there any commands that are commonly run with it to prevent interruption?
As I mentioned, I am working on this for a course assignment at the moment.
This however doesn't violate any of its rules.
I'm just trying to find a good starting point for these examples.
I don't have one for stdbuf
, and I dislike the rudimentary one I was using for nohup
.
kav
(173 rep)
Mar 27, 2015, 09:37 PM
• Last activity: May 7, 2025, 05:31 PM
0
votes
0
answers
34
views
php script: shell_exec('nohup /usr/bin/python3 -u /location/script.py > /location/script.log & : Script terminates after 5 seconds but should not
I have a PHP Script with the command `shell_exec('nohup /usr/bin/python3 -u /location/script.py > /location/script.log &');` This PHP Script is called via ajax, the script runs, logfile is created, output is written to logfile, but the python script terminates after 5-6 seconds. The same command run...
I have a PHP Script with the command
shell_exec('nohup /usr/bin/python3 -u /location/script.py > /location/script.log &');
This PHP Script is called via ajax, the script runs, logfile is created, output is written to logfile, but the python script terminates after 5-6 seconds. The same command runs flawlessly in a SSH terminal.
The PHP is an Ajax-called script, there is no issue with the Ajax call. I can tell because the logfile is created and the first few output lines are correct, but then the python script terminates.
Whatever I did, it did not change, I tried prefixing with sudo -u username
and sudo -u www-data
, appending & disown &
, changed the user that runs apache2, but all attempts didn't change the result. The python3 code is working correctly, because i can manually start the command from SSH and it runs flawlessly. There is no stacktrace in the logfile, it just stops. It vanishes from ps aux | grep script
I need the Ajax method of calling the script, what can I do?
Running on Debian 12
cli
(1 rep)
Apr 13, 2025, 02:57 PM
• Last activity: Apr 13, 2025, 04:01 PM
1
votes
1
answers
37
views
Process run with nohup gets killed on client_loop: send disconnect: Broken pipe
I have observed the following and want to understand why: First, I run a Node server that listens on a port on a remote server using: ``` nohup my-app & ``` Next there are two cases: 1. I logout of the remote server using `exit`. I call this graceful logout 2. I do not logout gracefully but instead...
I have observed the following and want to understand why:
First, I run a Node server that listens on a port on a remote server using:
nohup my-app &
Next there are two cases:
1. I logout of the remote server using exit
. I call this graceful logout
2. I do not logout gracefully but instead have a forceful logout due to my local machine going to sleep after extended period of inactivity or something like that. In this case I see a message printed on the screen that says client_loop: send disconnect: Broken pipe
When I log back into the machine, in case 1 the process is still running but in case 2 it is terminated. Why?
And to further add a twist, case 1 and case 2 behave same on Mac but not in Windows (using WSL2). I am very curious to understand what's going on here and why do Windows (WSL2) and Mac behave differently.
morpheus
(135 rep)
Mar 8, 2025, 12:38 AM
• Last activity: Mar 8, 2025, 06:58 PM
801
votes
6
answers
433924
views
Difference between nohup, disown and &
What are the differences between $ nohup foo and $ foo & and $ foo & $ disown
What are the differences between
$ nohup foo
and
$ foo &
and
$ foo &
$ disown
Lesmana
(28027 rep)
Nov 9, 2010, 04:16 PM
• Last activity: Dec 25, 2024, 11:40 PM
0
votes
0
answers
39
views
how make nohup log command it self
I use `nohup` like: ```bash nohup ./cmd.sh --para=abc > ${PWD}/cmd_$(date "+%Y%m%d_%H%M_%S").log 2>&1 & ``` How can I make the output log file include the command itself, i.e. the `./cmd.sh --para=abc > ${PWD}/cmd_$(date "+%Y%m%d_%H%M_%S").log` part could be found in the log file (say `cmd_20241030_...
I use
nohup
like:
nohup ./cmd.sh --para=abc > ${PWD}/cmd_$(date "+%Y%m%d_%H%M_%S").log 2>&1 &
How can I make the output log file include the command itself, i.e. the ./cmd.sh --para=abc > ${PWD}/cmd_$(date "+%Y%m%d_%H%M_%S").log
part could be found in the log file (say cmd_20241030_1315_00.log
).
And any of the followings would be ok:
nohup ./cmd.sh --para=abc > ${PWD}/cmd_$(date "+%Y%m%d_%H%M_%S").log 2>&1 &
nohup ./cmd.sh --para=abc > /real/path/cmd_20241030_1315_00.log
./cmd.sh --para=abc > /real/path/cmd_20241030_1315_00.log
./cmd.sh --para=abc
furynerd
(21 rep)
Oct 31, 2024, 01:31 AM
• Last activity: Oct 31, 2024, 09:55 AM
0
votes
1
answers
474
views
Systemd kills all background processes after ssh session ends despite all the typical solutions
I have tried almost every solution on Stack Exchange (except for `nohup`; it seems like that is not an ideal loophole), but Systemd still kills my background processes. I also tried starting a Tmux session as a service using [this](https://unix.stackexchange.com/a/683051/662799) question, but the pr...
I have tried almost every solution on Stack Exchange (except for
nohup
; it seems like that is not an ideal loophole), but Systemd still kills my background processes.
I also tried starting a Tmux session as a service using [this](https://unix.stackexchange.com/a/683051/662799) question, but the processes started in Tmux still get killed after SSH is disconnected.
I am using Ubuntu 22.04, the desktop version. My monitor is disconnected, but I have not set up headless mode, so I am wondering if, in the desktop version, I have to keep the monitor connected after local login. Disconnecting the monitor could cause Systemd to infer that no user is active, per [this](https://lists.freedesktop.org/archives/systemd-devel/2013-May/011006.html) email archive.
aneeqaf
(1 rep)
Oct 21, 2024, 09:07 AM
• Last activity: Oct 21, 2024, 09:51 AM
1
votes
2
answers
1209
views
why websocat or wscat in background with nohup, disown, bg, &, &! don't work
The question is about running a bash script in background with the ability to logout and the process to continue. The issue is that websocat and wscat don't like when I close the terminal or logout....... **** NOTE : the script.sh does work when just do `script.sh` it have the execute permission and...
The question is about running a bash script in background with the ability to logout and the process to continue. The issue is that websocat and wscat don't like when I close the terminal or logout.......
**** NOTE : the script.sh does work when just do
script.sh
it have the execute permission and there is no error in the script
But when I'm trying nohup, disown, bg, &, &! ===>>>> all of those don't work :(
the content of script.sh
wscat -c "wss://somewebsocketstream" > file
when I run nohup appending output to nohup.out
and when I hit enter, the script.sh stops
nohup /script.sh
when I add &
I get + suspended (tty input) nohup
and .sh stops
when I add &!
whitout nohup
and only script.sh &
I get + suspended (tty input)
when I type any key on the keyboard
only script.sh &!
nothing happens, I see it in top but no activity :(
when I try bg script.sh
I get bg: job not found
when I try script.sh 2>&1 &
or script.sh &> /dev/null &
I get + suspended (tty input) 2>&1
John R
(125 rep)
May 18, 2022, 02:01 PM
• Last activity: Sep 13, 2024, 11:26 PM
1
votes
3
answers
1863
views
Bash script to start java spring boot application from SSH
I have a simple bash script file, which is triggered from pipeline job ( via SSH ). The **deploy.sh** contains more or less something like this: cd /apps && java -jar "-Dspring.profiles.active=prod " "-Dserver.port=$PORT " \ "/apps/$DEPLOYING_VERSION" & And some other logic which is not so important...
I have a simple bash script file, which is triggered from pipeline job ( via SSH ).
The **deploy.sh** contains more or less something like this:
cd /apps && java -jar "-Dspring.profiles.active=prod " "-Dserver.port=$PORT " \
"/apps/$DEPLOYING_VERSION" &
And some other logic which is not so important.
The pipeline via SSH executes:
bash /deploy.sh ${APPLICATION_NAME}
How to start **deploy.sh** in order the java -jar to be executed and separated from the **deploy.sh** in order my pipeline (job) to finish and in the same time, the java app to continue working on the machine?
I tried with
cd /apps && nohup java -jar "-Dspring.profiles.active=prod " "-Dserver.port=$PORT " \
"/apps/$DEPLOYING_VERSION" &
But the job stuck...
It will be cool to track and the exit from the java -jar, but it is not so important right now :)
ROZZ
(109 rep)
Nov 20, 2022, 04:33 PM
• Last activity: Jul 12, 2024, 06:49 AM
5
votes
2
answers
3943
views
Has “loginctl enable-linger user” made the UNIX classic “nohup” superfluous? Or have I misunderstood something?
I have read about `loginctl enable-linger user` and `/etc/systemd/logind.conf` `(KillExcludeUsers=user1 user2 user3)` [Oracle Linux: Configure Systemd to Enable User Processes to Continue to Run After Logout][1] > This tutorial shows you how to set up the `systemd` service manager on > an Oracle Lin...
I have read about
loginctl enable-linger user
and /etc/systemd/logind.conf
(KillExcludeUsers=user1 user2 user3)
Oracle Linux: Configure Systemd to Enable User Processes to Continue to Run After Logout
> This tutorial shows you how to set up the systemd
service manager on
> an Oracle Linux 8 system to ensure that services and processes that
> are started by a user continue after the user's session has ended.
Is the tool nohup
no longer needed? This functionality is now in systemd?
Sybil
(1983 rep)
Jun 12, 2024, 03:34 AM
• Last activity: Jun 13, 2024, 04:32 AM
0
votes
1
answers
143
views
Why is tee stopping in the middle when piped with nohup?
I'm running a script (in a Laravel project) that takes a few hours to complete. `nohup` to prevent it from exiting if my ssh session is disconnected. ```lang-shell nohup php artisan do:thing 2>&1 | tee ~/tmp ``` It starts off nicely, but after some time, I get a broken pipe error and it stops output...
I'm running a script (in a Laravel project) that takes a few hours to complete.
nohup
to prevent it from exiting if my ssh session is disconnected.
-shell
nohup php artisan do:thing 2>&1 | tee ~/tmp
It starts off nicely, but after some time, I get a broken pipe error and it stops outputing things, and tee
stops as well - the doc stops at that point.
I guess it's because the tee
process got killed by the ssh session drop? Can I prevent it?
Could I maybe disown the tee process and prevent ssh from terminating it?
Jeremy Belolo
(103 rep)
Jan 11, 2024, 07:16 AM
• Last activity: Jan 11, 2024, 08:38 AM
-1
votes
1
answers
200
views
Why does running rsync using nohup consume all of a system’s memory?
Through both testing and plenty of examples on various stack exchanges, its clear that running `rsync` using `nohup` causes memory exhaustion, while the same `rsync` job running via `screen` or an active SSH session does not cause memory exhaustion. Can anyone explain why this is? Also, is there a w...
Through both testing and plenty of examples on various stack exchanges, its clear that running
rsync
using nohup
causes memory exhaustion, while the same rsync
job running via screen
or an active SSH session does not cause memory exhaustion.
Can anyone explain why this is? Also, is there a way to achieve the same functionality as nohup
without screen
?
Tammer Ibrahim
(813 rep)
Dec 13, 2023, 04:21 PM
• Last activity: Dec 14, 2023, 10:24 AM
1
votes
2
answers
824
views
nohup won't show next prompt in SSH session
Assume that I am connected via SSH on a remote Ubuntu system. I want to execute a script there using `nohup` in case of potential connection drops and redirecting its log to a file. The command I run is nohup bash myscript.sh > log.txt & Then, the terminal prints the PID of the script and the messag...
Assume that I am connected via SSH on a remote Ubuntu system. I want to execute a script there using
nohup
in case of potential connection drops and redirecting its log to a file. The command I run is
nohup bash myscript.sh > log.txt &
Then, the terminal prints the PID of the script and the message:
> nohup ignoring input and redirecting stderr to stdout
The cursor freezes there and I am not able to execute other commands unless I exit the SSH session with Ctrl + D and relogin. I would prefer the SSH session to remain open and proceed to the next terminal prompt without the need to relogin. Is there any way to achieve this?
mgus
(259 rep)
Aug 20, 2020, 05:57 AM
• Last activity: Nov 28, 2023, 04:19 AM
-1
votes
1
answers
144
views
Can you make nohup.out get created only on errors without manually specifying the file name?
Can you make `nohup ./file $1 $2 &` (which runs `./file` in the background) create `nohup.out` only for errors and not for standard output? But please without manually specifying `nohup.out` i.e. nohup ./file $1 $2 >/dev/null 2>nohup.out & Also, the following states `nohup: redirecting stderr to std...
Can you make
nohup ./file $1 $2 &
(which runs ./file
in the background) create nohup.out
only for errors and not for standard output?
But please without manually specifying nohup.out
i.e.
nohup ./file $1 $2 >/dev/null 2>nohup.out &
Also, the following states nohup: redirecting stderr to stdout
and redirects neither output nor errors. I assume it's because it tells it to redirect errors to output but output is redirected to /dev/null:
nohup ./file $1 $2 >/dev/null &
LWC
(113 rep)
Nov 27, 2023, 10:36 AM
• Last activity: Nov 27, 2023, 12:40 PM
22
votes
2
answers
23196
views
How to make nohup not create any output files and so not eat all space?
So I start some server with `nohup ./myServer &`, I actually have loging mon my server so I do not need any help from nohup with it, but any way I get `66GB` `nohup.out` file in fiew days and that is a problem. I want just to start an app, leve it working on its own. So how to make nohup not create...
So I start some server with
nohup ./myServer &
, I actually have loging mon my server so I do not need any help from nohup with it, but any way I get 66GB
nohup.out
file in fiew days and that is a problem. I want just to start an app, leve it working on its own. So how to make nohup not create any output files and so not eat any space?
Kabumbus
(363 rep)
Oct 21, 2011, 12:33 AM
• Last activity: Oct 31, 2023, 08:29 PM
1
votes
1
answers
115
views
Background process is not being terminated by SIGINT when executed via ssh
I was experimenting with ssh, nohup, bg etc. I started a tail process in remote using ``` $ ssh remotehost '{ nohup tail -f ut.log &> /dev/null /dev/null < /dev/null &} && echo $! ``` ``` $ ps -o pid,tid,ppid,pgid,sid,class,nice,rtprio,pri,sched,cmd,tty,tpgid,cgroup,stat,flags,sig,sigcatch,sigignore...
I was experimenting with ssh, nohup, bg etc. I started a tail process in remote using
$ ssh remotehost '{ nohup tail -f ut.log &> /dev/null /dev/null < /dev/null &} && echo $!
$ ps -o pid,tid,ppid,pgid,sid,class,nice,rtprio,pri,sched,cmd,tty,tpgid,cgroup,stat,flags,sig,sigcatch,sigignore,sigmask,euid,ruid,egid,rgid,ouid -p 28111
PID TID PPID PGID SID CLS NI RTPRIO PRI SCH CMD TT TPGID CGROUP STAT F PENDING CAUGHT IGNORED BLOCKED EUID RUID EGID RGID OWNER
28111 28111 24889 28111 24889 TS 0 - 19 0 tail -f ut.log pts/45 28128 12:pids:/user.slice/user-28 S 0 0000000000000000 0000000000000000 0000000000000001 0000000000000000 28889 28889 10005 10005 28889
Why it is happening like that?
**EDIT:** I think it may be because of the sigignore mask present in the former. But both processes are started by exact same command. Then how both processes have different masks? I used nohup
, hence I expect mask for SIGHUP
to be present. But how did that process got other masks? Did ssh
added them? How can I get rid of that?
Sourav Kannantha B
(121 rep)
Oct 1, 2023, 04:30 AM
• Last activity: Oct 1, 2023, 06:04 AM
2
votes
2
answers
31117
views
Can not stop nohup process
I am trying to run a very complex statistical model that I expected to take a very long time, so I used ```nohup``` to run the process. However, I underestimated the length and it would take like 260+ days to complete so I want to cancel the process. I attempted using ``` ps aux |grep nohup``` to ge...
I am trying to run a very complex statistical model that I expected to take a very long time, so I used
to run the process. However, I underestimated the length and it would take like 260+ days to complete so I want to cancel the process.
I attempted using ps aux |grep nohup
to get the PID, I get the following output:
\ 4920 0.0 0.0 14224 972 pts/15 S+ 01:53 0:00 grep nohup
When I use kill -9 14224
I am told there is no process. So I used ps -ef | grep beast-2
as I do not remember the entire command I entered. Pulls up the following:
144039 1 0 Jul22 pts/12 00:00:00 /bin/sh /opt/anaconda3/bin/beast-2 -seed 777 -working -overwrite -instances 3 -threads 3 -beagle_SSE /home//XML/ST736_MTBD1.xml
I use kill -9 144039
and check again but the process is still running underneath a new PID.
What can I do?
MicroBiostat
(23 rep)
Jul 23, 2021, 02:30 PM
• Last activity: Sep 13, 2023, 05:31 PM
Showing page 1 of 20 total questions