Quitting a CGI App on Linux
So you followed my advice in Speed Up CGI Launch Times and now you’re wondering: how do I quit my app so I can install an update?
If you want to quit all instances of your app named MyAppName:
- Login using your SSH account.
- Quit the app with the command:
pkill MyAppName
If you want to quit a specific instance by PID:
- Login using your SSH account.
- Find the app PID with the command:
pidof MyAppName
- Quit the app with the command:
kill PID
Update: Hat tip to Jay Madren for pointing out that you can quit an app on Linux by name using pkill. Check out his other tips below.
Most WE users who are deploying CGI apps to a VPS are no doubt deploying them to web hosts that use Linux servers. When you sign up for a VPS your web host will provide you with a SSH account which you can use to login and run commands. You do not require root access to find and quit your app. Any CGIs or apps that launch on your VPS should launch under your account. (If you have a Windows or Mac VPS or stand alone server then you should be able to remote into the full GUI and use the normal tools to find and quit any process.)
The command to quit your app is kill and it requires the process identifier, or PID, of your app. There are two ways to find the PID.
- Type the command:
pidof MyAppName
- Type the command:
top
The first will return the PID of your app. The second will return the top processes. Look for your app in the far right column, and find its PID in the far left column.
Once you have the PID simply type: kill PID
Now you can install your update. Note that it is possible that a user could launch your app again while you start transferring files, so you might have to kill the process again (it will have a new PID) or perform the update at a different time when your site has no traffic.
Instead of using pidof or top, you can use pkill to kill the process by name:
pkill MyAppName
If your app runs under a different user account than what you use for ssh, then you can add the -u switch like so:
pkill -u username MyAppName
And you can further automate this if your local machine is a Mac or Linux box (may be possible on Windows with some utility) by running ssh with the above command:
ssh root@yourdomain.com pkill -u username MyAppName
Of course, change “root” to whatever account you use to connect with ssh. ssh will prompt for your password, then run the command and disconnect.