CTRL-Z and fg: my new(old) best friend

Tech No Comments

As an operations engineer, I get to touch a lot of systems and a lot of different apps on those systems on a daily basis. This means a lot of log following, debugging, fixing, and more log following. Something that I love but often forget to use is background jobs. Background jobs work like windows on your desktop but for the command line. You need to switch between your text editor (vi), the log tail, and the command prompt. You could open three connections to the machine or you could use the background/foreground functionality built in to linux/bsd. Here is what you do:

You need to follow /var/logs/app.log, you need to edit /etc/app/app.conf, and you need to execute /usr/bin/app -debug to fix your problem.

First you run tail -f /var/logs/app.log and then hit ‘ctrl+z’ to suspend this task and return to the prompt. You should see something like:
[1]+ Stopped tail -f /var/logs/app.log

The number at the beginning is the job number. That is how you get back to the process.

Now you run vi /etc/app/app.conf and hit ‘ctrl+z’ This will be assigned job number 2:
[2]+ Stopped vi /etc/app/app.conf

Now you can use the ‘fg’ command to ‘foreground’ a job. So if you want to pull the log back up you do ‘fg 1′ and the log job will come back. Use ‘ctrl+z’ to go back to the command line and run ‘fg 2′ to pull up your app.conf. It’s that easy. If you ever forget what jobs are which you can run ‘jobs’ to display all running jobs:

[1]+ Stopped tail -f /var/logs/app.log
[2]+ Stopped vi /etc/app/app.conf

It’s that freaking easy. Enjoy…