Restart a process
Stop and re-launch a running process — useful for picking up config changes, recovering from leaks, and orchestrated reloads.
How to restart a process in each shell
Bashunix
kill -TERM $PID && wait $PID 2>/dev/null; ./myapp &Zshunix
kill -TERM $PID && wait $PID 2>/dev/null; ./myapp &Fishunix
kill -TERM $PID; and wait $PID 2>/dev/null; ./myapp &PowerShellwindows
Stop-Process -Id $pid -Force; Wait-Process -Id $pid -ErrorAction SilentlyContinue; Start-Process .\myapp.exe`Restart-Service` is for WINDOWS SERVICES only (entries in services.msc), NOT arbitrary processes. Wrap stop + wait + start manually for `.exe` processes.
cmd.exewindows
taskkill /F /PID %PID% && start /b myapp.exeNo `wait` primitive — `taskkill /F` returns once the kill is QUEUED, not once the PID is gone. For port-binding apps poll `netstat -an | findstr :PORT` before relaunching.
Equivalents listed for Bash, Zsh, Fish, PowerShell, cmd.exe.
Gotchas & notes
- The naive kill-and-relaunch idiom has a RACE on listening sockets: if `myapp` binds to a specific port and you re-launch before the kernel fully reaps the socket, the new instance fails with `EADDRINUSE` and sits in `TIME_WAIT` for ~60s. Mitigations: bind with `SO_REUSEADDR`/`SO_REUSEPORT` (app-level, requires source change), wait out TIME_WAIT, or let a service manager (systemd/launchd) handle the restart — they pre-allocate the socket via socket activation and pass it across the restart.
- For services managed by systemd: `sudo systemctl restart nginx` is atomic — sends SIGTERM, waits up to `TimeoutStopSec` (90s default), escalates to SIGKILL if needed, then re-launches. For services managed by launchd (macOS): `sudo launchctl kickstart -k system/com.apple.foo` — the `-k` means "kick: kill then restart". For sysvinit: `sudo service nginx restart` (legacy wrapper). None of these require PID lookup — they read the unit's PIDFile or cgroup directly.
- pwsh `Restart-Service nginx` ONLY restarts entries in the Windows Service Control Manager (`Get-Service` lists them) — it errors `Cannot find any service...` for a plain `.exe` you launched yourself. For arbitrary processes, the wrapper is: `$p = Get-Process myapp; Stop-Process $p -Force; Wait-Process -Id $p.Id -ErrorAction SilentlyContinue; Start-Process myapp.exe`. The `Wait-Process` with `SilentlyContinue` returns immediately if the PID is already gone (otherwise it throws "no such process" — common race condition with `-Force`).
- Production process managers handle restart cleanly: `pm2 restart myapp` (Node — keeps log files, restart counter, supports cluster-mode rolling restart); `supervisorctl restart myapp` (supervisord — Python-friendly); `docker restart container_id` (Docker — respects `STOPSIGNAL` + `STOPTIMEOUT`); `kubectl rollout restart deployment/myapp` (K8s — rolls pods, respects PodDisruptionBudget). All three handle graceful-shutdown timeout + signal escalation + post-restart healthcheck — homebrew shell scripts skip those and produce subtle outages.
Related commands
Related tasks
- Send a signal to a process— Deliver a specific Unix signal to a process — useful for graceful shutdown, config reload, and triggered behaviour.
- Kill a process by name— Terminate every running process whose executable matches a given name.
- Wait for a process to finish— Block until a given process exits — useful for sequencing, dependency-management scripts, and orchestrated shutdowns.
- Run a command in the background— Launch a long-running command without blocking the current shell session.