Day 5
Environment Variables
Viewing environment variables:
printenv
– Viewing the environment variables.
printenv HOME
– Only variable of HOME will be displayed, for example something like this: “/home/philipp”.
echo $HOME
– Does the same thing.
Environment values are case sensitive.
Creating environment variables:
export VAR="value"
For example:
export EDITOR="vi"
export TZ="US/Pacific"
If this variable already exists, this will override.
Remove environment variables:
unset VAR
For example:
unset TZ
man date
– Look into the manual of the “date” command. Scroll down to the bottom where there is an ENVIRONMENT section.
NOTE: Those changes are only valid for the currently running session. If you want to make these changes persist, you need to write them to for example .bash_profile. So, for example, add “export TZ=”US/Pacific” to the file.
Process and Job Control
ps
– Display the currently running processes.
ps -e
– Everything, all processes.
ps -f
– Full format listing.
ps -u username
– Display username’s processes.
ps -p pid
– Display information for a specific PID (Process Identification Number, example 1530).
ps -ef
– Dsiplay all processes, full.
ps -eH
– Display a process tree.
ps -e --forest
– Display a process tree.
ps -ef | less
– A full listing piped to less.
UID – User.
PID – Process Identification.
PPID – Parent Process Identification.
STIME – Time the process started.
CMD – Command name.
Other ways to view processes:
pstree
– Display processes in a tree format.
top
– Interactive process viewer.
htop
– Interactive process viewer.
Backgound and foreground processes:
command &
– Start command in background.
Ctrl-c
– Kill the foreground process.
Ctrl-z
– Suspend the foreground process.
bg [%num]
– Background a suspended process.
fg [%num]
– Foreground a background process.
kill
– Kill a process by job number or PID.
jobs [%num]
– List jobs.
Killing processes:
Ctrl-c
– Kills the foreground process.
kill [-sig] pid
– Send a signal to a process.
kill -l
– Display a list of signals.
kill 123
kill -15 123
kill -TERM 123
kill -9 123
For example:
./long-running-program &
– Start job in the background.
ps -p 2373
– Display information for this job.
jobs
– List jobs.
jobs %1
fg
fg %2
– Bring job number 2 to the foreground.
kill %1
– Kill job number 1.
kill -l
– To see al the signals we can send to the kill command.
kill -9 [%num]
– For a hard to kill process.
kill -KILL
Scheduling Repeated Jobs with Cron
cron
– A time based job scheduling service.
crontab
– A program to create, read, update, and delete your job schedules.
Use cron to schedule and automate tasks.
* * * * * command
*1 Minute (0-6)
*2 Hour (1-12)
*3 Day of the Month (1-31)
*4 Month of the Year (0-23)
*5 Day of the Week (0-59)
Example crontab entry:
0 7 * * 1 /opt/sales/bin/weekly-report
– Run every Monday at 07:00.
0,15,30,45 * * * * /opt/acme/bin/15-min-check
– Run every 15 minutes.
*/15 * * * * /opt/acme/bin/15-min-check
– Another way to do the same thing.
0-4 * * * * /opt/acme/bin/first-five-mins
– Run for the first 5 minutes of the hour.
Crontab Shortcuts:
@yearly
– 0 0 1 1 *
@annually
– 0 0 1 1 *
@monthly
– 0 0 1 * *
@weekly
– 0 0 * * 0
@daily
– 0 0 * * *
@midnight
– 0 0 * * *
@hourly
– 0 * * * *
For more shortcuts run man cron
.
Using the crontab command:
crontab file
– Install a new crontab from file.
crontab -l
– To list all cron jobs.
crontab -e
– To edit cron jobs.
crontab -r
– Remove all of your cron jobs.
Switching Users and Running Commands as Others
su [username]
– Change user ID or become superuser.
su - [username]
– A hyphen is used to provide an environment similar to what the user would expect had the user logged in directly.
su -c 'command' [username]
– Specify a command to be executed.
whoami
– Displays the effective username.
sudo
– Execute a command as another user, typically the superuser (sudo – superuser do).
sudo -l
– List available commands.
sudo command
– Run command as root.
sudo -u root command
– Same as above.
sudo -u user command
– Run as user.
sudo su
– Switch to the superuser account.
sudo su -
– Switch to the superuser account with root’s environment.
sudo su - username
– Switch to the username account.
sudo -s
– Start a shell or switch to the root account.
sudo -u root -s
– Same as sudo -s
.
sudo -u user -s
– Start a shell as user.
sudo /etc/init.d/orcale start
– Start application called oracle.
visudo
– Changing the sudo configuration, i.e. the /etc/sudoers file.
Sudoers Format:
user host=(users)[NOPASSWD:]commands
adminuser ALL=(ALL) NOPASSWD:ALL
philipp linuxsvr=(root) /etc/init.d/oracle
Shell History
1. Executed commands are added to the history.
2. Shell history can be displayed and recalled.
3. Shell history is stored in memory and on disk:
- ~/.bash_history
- ~/.history
- ~/.histfile
history
– Displays the shell history.
HISTSIZE
– Controls the number of commands to retain in history.
export HISTSIZE=1000
– To increase the size of history.
!N
– Repeat command in line number N.
!!
– Repeat the previous command line.
!string
– Repeat the most recent command starting with “string”.
Format: !:N
!
– Represents a command line (or event).
!
= The most recent command line.
!
= !!
:N
– Represents a word on the command line.
0 = command, 1 = first argument, etc.
!^
– Represents the first argument.
!^
= !:1
!$
– Represents the last argument.
For example:
head files.txt sorted_files.txt notes.txt
!^ = files.txt
!$ = notes.txt
Searching shell history
Ctrl-r
– Reverse shell history search.
Enter
– Execute the command.
Arrows
– Change the command
Ctrl-g
– Cancel the search
Tab completion
Autocomplete with tab works for: commands, files, directories, paths, environment variables, usernames (~).
Installing Software on Linux
Packages
- A collection of files
- Data / Metadata: Package description, Version, Dependencies.
Package Manager
- Installs, upgrades, and removes packages.
- Manages dependencies.
- Keeps track of what is installed.
Installing Software on RPM Distros
- RedHat
- CentOS
- Fedora
- Oracle Linux
- Scientific Linux
yum command line utility
yum search string
– Search for string.
yum info [package]
– Display info of package.
yum install [-y] package
– Install package.
yum remove package
– Remove package.
Alternatively use rpm
.
rpm -qa
– Lista ll installed packages.
rpm -qf /path/to/file
– List the file’s package.
rpm -ql package
– List package’s files.
rpm -ivh package.rpm
– Install package.
rpm -e package
– Erase (uninstall) package.
Sudo privileges are often necessary to install packages. For example:
yum info inkscape
su -
– Switch to root user (sudo).
yum install inkscape
– Install Inkscape.
y
and Enter.
You can also type yum install -y gimp
, so you will not have to answer with a y interactively.
yum remove gimp
– Remove gimp.
yum search dropbox
– Search if DropBox is in the package manager of the current distribution.
So you have to down the DropBox .rpm file from dropbox.com and install it like so:rpm -ivh dropbox.rpm
– -ivh
for additional progress.
rpm -qa | sort | less
– Show all installed packages on the system.
which which
– Display what program which belongs to.
rpm -ql which
– Display what other things are in a particular package.
Installing software on DEB Distros
- Debian
- Linux Mint
- Ubuntu
Debian based distributions use a package manager called APT.
apt-cache search string
– Search for string.
apt-get install [-y] package
– Install package.
apt-get remove package
– Remove package, leaving configuration.
apt-get purge package
– Remove package, deleting configuration.
apt-cache show package
– Display information about package.
Additionally the dpkg command can be used to communicate with the package manager.
dpkg -l
– List installed packages.
dpkg -S /path/to/file
– List file’s package.
dpkg -L package
– List all files in package.
dpkg -i package.deb
– Install package.
NOTE: .deb file is the package ending for Ubuntu.