Processes, Memory and Disk
A process is a running program. Servers often need you to check what's running, what's using memory, and whether the disk is full.
Servers run many programs at once. Knowing how to see what's running, what's using memory, and how full the disk is helps you fix the most common production problems - a crashed process, a memory leak, or a disk full of logs.
Example
ps aux | grep node # find running node processes
top # live list of busy processes (q to quit); htop is a nicer version
kill 4821 # ask process 4821 to stop
kill -9 4821 # force it to stop (last resort)
df -h # disk space, in human-readable sizes
du -sh logs/ # how much space a folder uses
free -h # memory (RAM) use
uptime # how long it's been running, and the loadThe numbers you see depend on your computer, so they aren't shown here. In df -h watch the Use% column - a full disk is one of the most common reasons a server suddenly stops working (often because of big log files).
Running things in the background
Example
$ sleep 30 &
[1] 4821 # job number and process id (yours will differ)
$ jobs
[1]+ Running sleep 30 &
$ kill %1
$ jobs
[1]+ Terminated sleep 30command &- run in the backgroundjobs,fg,bg- list jobs, bring one forward, continue one in the backgroundnohup command &- keep running after you log out. For real servers, use a service (next lesson) instead
Environment variables
Example
$ export APP_ENV=production
$ echo "Running in $APP_ENV mode"
Running in production mode
$ env | grep APP_
APP_ENV=productionKey points
- ps, top and kill manage processes.
- df -h and du -sh check disk space.
- export sets environment variables.
📝 Test yourself
1. Which command shows disk space in readable sizes?
2. What does adding & at the end of a command do?
3. Fill in the command that stops a process by its id:
4821
