Home > Net >  How to exit putty without killing powershell process?
How to exit putty without killing powershell process?

Time:12-03

I am hosting a discord bot on a linux server using powershell. But whenever I exit putty it kills the powershell process. How do I keep it running?

CodePudding user response:

On the linux side, use & to run the process in the background, and the nohup command to keep running when your user exits a shell (putty session):

# Run your command 
[user@server ~]$ nohup pwsh -Command 'Do-Thing -Foo a -Bar b' &

# Or run a script
[user@server ~]$ nohup pwsh -File /path/to/script.ps1 &

It will show up as a job in jobs, but only until you disconnect:

[user@server ~]$ jobs
[1]   Running         nohup pwsh -Command 'Sleep -100' &

# bring the job to the foreground and kill it, for example
[user@server ~]$ fg 1
[user@server ~]$ ^C

Once you disconnect, the job will run on its own. Use ps to find the running process and kill it if needed:

[user@server ~]$ ps -e | grep pwsh
30975 ?        00:00:01 pwsh

[user@server ~]$ kill 30975

If you want something more robust (like auto-restart if crashed), you can set it up to run as a service, but the steps for that can vary between flavors of linux.

CodePudding user response:

In powershell use:

powershell -noExit "cf ssh app_name"

It is possible to set a powershell function to shorten it:

function psne { powershell -NoExit $args[0]}

Then run

psne "cf ssh app_name"
  • Related