Debugging workflow & editing files
The TechWorld with Nana video builds its whole lesson around one realistic problem: an app on a server can't connect to its database. Working through a problem like that uses most of the commands from this topic, in a sensible order.
A step-by-step debugging routine
- Where am I?
uname -ashows the kernel and architecture,cat /etc/os-releaseshows the distro and version,hostnameandwhoamiconfirm the machine and user. It sounds basic, but fixing the wrong server is a classic mistake. - Is it running?
systemctl status myapporps aux | grep myapp. If it keeps restarting, the status output shows the last log lines. - What do the logs say?
journalctl -u myapp -n 50, ortail -n 100 /var/log/myapp/app.log, then narrow it down:grep -i "error\|refused\|timeout" app.log. - Is the config right? Find it with
find / -name "*.conf" 2>/dev/null | grep myapp, read it withcatorless, and compare against a known good copy withdiff app.conf app.conf.backup. In the video the bug is a wrong database host in the config. - Can it reach what it needs?
curl -I http://localhost:8080fetches only the HTTP headers, a fast "is the web server answering?" test.ping db-serverandnc -zv db-server 5432check the network path and whether the database port is open. - Fix, restart, verify. Edit the file,
sudo systemctl restart myapp, and watch the log withtail -fto see it recover.
cp app.conf app.conf.bak. If the change makes things worse, you can put the original back in seconds.Editing files: nano and Vim
On servers there's no graphical editor, so you edit in the terminal. nano is the easy one: the shortcuts are listed at the bottom (Ctrl+O saves, Ctrl+X exits). Vim is installed almost everywhere, so it's worth knowing the survival basics. Vim has modes: in normal mode keys are commands, in insert mode keys type text.
vim fileopens it in normal mode.ienters insert mode so you can type;Escgoes back to normal mode.:wsaves,:qquits,:wqsaves and quits,:q!quits and throws away changes./wordsearches,dddeletes a line,uundoes.
If Vim seems to ignore you, you're almost certainly in the wrong mode: press Esc and try again.
Shell scripts: automating the routine
Any commands you type more than twice belong in a script. A script is a text file of commands that runs top to bottom:
#!/bin/bash
# backup.sh - archive a folder with today's date
set -e # stop at the first failing command
SOURCE="$1" # first argument
DEST="/backups/$(date +%F).tar.gz"
if [ -z "$SOURCE" ]; then
echo "usage: ./backup.sh folder" >&2
exit 1
fi
tar -czf "$DEST" "$SOURCE"
echo "saved $DEST"
$ chmod +x backup.sh
$ ./backup.sh /srv/app
- The first line, the shebang
#!/bin/bash, says which program runs the file. $1,$2… are the arguments;$(command)inserts a command's output.- Every command returns an exit code: 0 means success, anything else is an error.
$?holds the last one, andset -emakes the script stop as soon as one command fails, instead of carrying on in a broken state. for f in *.log; do gzip "$f"; doneloops over files.