ps ux
$! is the PID of the last backgrounded process.
kill -0 $PID checks whether it's still running.
$$ is the PID of the current shell.
$ ps ax | grep firefox
2222 ? S 0:00 /bin/sh /usr/lib/firefox-3.6.9/firefox
2231 ? Sl 514:36 /usr/lib/firefox-3.6.9/firefox-bin
30290 pts/2 S+ 0:00 grep --color=auto firefox
#include <iostream>
#include <Windows.h>
#include <tlhelp32.h>
#include <tchar.h>
std::uint32_t get_proc_id(const std::wstring& name)
{
auto result = 0ul;
auto snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapshot == INVALID_HANDLE_VALUE)
return result;
auto pe32w = PROCESSENTRY32W{}; // use the wide version of the struct
pe32w.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(snapshot, &pe32w))
return CloseHandle(snapshot), result;
while (Process32NextW(snapshot, &pe32w))
{
if (name == pe32w.szExeFile) // use std::wstring's operator, not comparing pointers here
{
result = pe32w.th32ProcessID;
break;
}
}
CloseHandle(snapshot);
return result;
}
int main()
{
const auto pid = get_proc_id(L"explorer.exe");
std::cout << pid;
std::cin.get();
}
lsof -i tcp:3000
kill -9 pId