Graduate Program KB

Environments and Processes


Environments

View your environment variables with printenv
Edit your Environment Variables with sudo vim /etc/environment/. Can edit PATH here too.


Processes

There are always processes running in Linux.
A process is any sort of command that's currently running.
You can get a processes snapshot with ps to see current processes running (a broad view of only what is running for you, the user)
To see everything that is running on the entire system use ps aux.
ps aux is often used with grep to search or used with less for quick scrolling.

ps aux | grep "search string"
ps aux | less

Stopping a Process

Rather than killing a process with CTRL + D or CTRL + C. We can use CTRL + Z to stop the process.
It can then be continued in the background with bg.

dempsey$ sleep 1000
^Z
[1]+  Stopped                 sleep 1000
dempsey$ bg 1
[1]+ sleep 1000 &
dempsey$ ps
    PID TTY          TIME CMD
 100058 pts/1    00:00:00 bash
 100304 pts/1    00:00:00 sleep
 100308 pts/1    00:00:00 ps
  • bg means background
  • fg means foreground
  • Very helpful for moving processes between bg and fg.

Exit Codes

Success is shown as a 0
Anything else means the program did not finish successfully

Process Operators

&&

You can use && to have commands run if the previous command was successful. For example below, the second command will only run if the command previous returned a 0.

touch file.txt && date >> file.txt

||

The next clause only runs if the previous clause was not successful. Below will output hi.

false || echo hi

Below won't have any output.

true || echo hi

;

Runs commands in sequence no matter what.

true ; false ; echo hi

Above outputs hi.

Subcommands

Is done within a command with $( )
Some basic use cases

echo current date is $(date)
echo my name is $(whoami)

Another use case:

echo $(date +%x) - $(uptime) >> file.txt

Return