Professional Documents
Culture Documents
4 - Introduction To Linux II
4 - Introduction To Linux II
Introduction to Linux II
Topic 04
Recap
Foreground
Background
• The above example displays all files where the name starts with
file and end with .txt.
// AAI • Cloud Technologies (CAI2C04) TEMASEK POLYTECHNIC I School of Informatics & IT 9
Foreground Processes
• Running processes in
the background allows you to
perform other tasks at the same Foreground
time.
• If the background process
requires any keyboard input, it
waits.
Background
• Using the same example to list all files in a directory, the following
command is issued:
ls file*.txt &
• Here, if the ls command wants any input (which it does not), it goes
into a stop state until we move it into the foreground and give it the
data from the keyboard. At this point you will see a blinking cursor.
• Press the Enter key and you will see the following:
• One of the most commonly used flags for ps is the -f ( f for full )
option, which provides more information as shown in the following
example:
Process ID
• When a process is killed, a ps listing may still show the process with
a Z state. This is a zombie or defunct process. The process is dead
and not being used.
• These processes are different from the orphan processes. They have
completed execution but still find an entry in the process table.
• By running top command, you will see the information below in the
terminal. You can end top by pressing the ‘Q’ key.
• You can use the vi editor to edit an existing file or to create a new file
from scratch.
• You can also use this editor to just read a text file.
• The following table lists out the basic commands to use the vi editor:
Command Description
vi <filename> Creates a new file if it already does not exist,
otherwise opens an existing file.
vi -R <filename> Opens an existing file in the read-only mode.
view <filename> Opens an existing file in the read-only mode.
• When working with vi editor, there are two modes to work on:
• Command Mode − This mode enables you to perform
administrative tasks such as saving the files, executing the
commands, moving the cursor, cutting (yanking) and pasting the
lines or words, as well as finding and replacing. In this mode,
whatever you type is interpreted as a command.
• Insert Mode − This mode enables you to insert text into the file.
Everything that's typed in this mode is interpreted as input and
placed in the file.
• To move around within a file without affecting your text, you must
be in the command mode (press Esc twice).
• The following table lists out a few commands you can use to move
around one character at a time:
Command Description
k Moves the cursor up one line
j Moves the cursor down one line
h Moves the cursor to the left one character position
l Moves the cursor to the right one character position
Case-sensitive
commands
• To edit a file, you need to be in the insert mode. There are many
ways to enter the insert mode from the command mode.
Command Description
i Inserts text before the current cursor location
l Inserts text at the beginning of the current line
a Inserts text after the current cursor location
A Inserts text at the end of the current line
o Creates a new line for text entry below the cursor
location
O Creates a new line for text entry above the cursor
location
• You can copy lines or words from one place and then you can paste
them at another place using the following commands.
Command Description
yy Copies the current line.
yw Copies the current word from the character the
lowercase w cursor is on, until the end of the word.
p Puts the copied text after the cursor.
P Puts the yanked text before the cursor.
• You can connect two commands together so that the output from
one program becomes the input of the next program. Two or more
commands connected in this way form a pipe.
• To make a pipe, put a vertical bar (|) on the command line between
two commands.
• When a program takes its input from another program, it performs
some operation on that input, and writes the result to the standard
output (terminal screen). It is referred to as a filter.
• The grep command searches a file or files for lines that have a
certain pattern. The syntax is:
grep <pattern> <file(s)>
• The name "grep" comes from the ed (a Unix line editor) command
g/re/p which means “globally search for a regular expression and
print all lines containing it”.
• A regular expression is either some plain text (a word, for example)
and/or special characters used for pattern matching.
that can be used with grep -v Prints all lines that do not match pattern.
-n Prints the matched line and its line
command. number.
-l Prints only the names of files with
matching lines (letter "l")
-c Prints only the count of matching lines.
-i Matches either upper or lowercase.
Flags/Parameters Description
-n Sorts numerically (example: 10 will sort after 2), ignores blanks
and tabs.
-r Reverses the order of sort.
-f Sorts upper and lowercase together.
+x Ignores first x fields when sorting.
• The sort option +4n skips four fields (fields are separated by blanks)
then sorts the lines in numeric order.
• Most Unix system commands take input from your terminal and
send the resulting output back to your terminal.
• A command normally reads its input from the standard input, which
happens to be your terminal by default.
• Similarly, a command normally writes its output to standard output,
which is again your terminal by default.
• For example, to count the number of lines in the file users generated above, you
can execute the command as shown here.
• You can count the number of lines in the file by redirecting the standard input of
the wc command from the file testls.
• Sometimes you will need to execute a command, but you don't want
the output displayed on the screen.
• In such cases, you can discard the output by redirecting it to the file
/dev/null. For example:
ls –l > /dev/null
• The file /dev/null is a special file that automatically discards all its
input.
• In Linux, almost all the streams are treated as if they were files.
• Just like you can read/write a file, you can read/write data from
these streams.
• You can access these streams using unique values that are assigned
to each one of them.
• The three special streams and their unique value are listed here:
• If you run a command and there are error messages, you can save all error
messages into a file.
• For example, the following find command look for all files with .conf
extension under the /root directory. Any standard error from this execution will
be saved in the file find_error.txt.
find /root -name "*.conf" 2>find_error.txt
• The “2” in “2>” is the STDERR.
• As an example, you can redirect STDOUT to a file named results.txt and STDERR
to file named find_errors.txt:
• You can also send both STDOUT and STDERR to a file named output.txt using
“&>”:
• Crontab (cron table) is a text file that specifies the schedule of cron
jobs.
• There are two types of crontab files:
• system-wide crontab files and
• individual user crontab files.
• Users' crontab files are named according to the user’s name, and
their location varies by operating systems.
• In Red Hat based distributions such as CentOS and Fedora, crontab
files are stored in the /var/spool/cron directory, while on Debian and
Ubuntu files are stored in the /var/spool/cron/crontabs directory.
// AAI • Cloud Technologies (CAI2C04) TEMASEK POLYTECHNIC I School of Informatics & IT
What is Crontab File?
• Although you can edit the user crontab files manually, it is recommended to use the crontab
command.
• The /etc/crontab file and the scripts inside the /etc/cron.d directory are system-wide crontab
files that can be edited only by the system administrators.
• The example below shows the content of a crontab file in Ubuntu. Notice that the crontab
filename is the username of the user, fjor.
• Each line in the user crontab file contains six fields separated by a
space followed by the command to be run.
* * * * * command(s)
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
• The first five fields may contain one or more values, separated by a comma or a range of values
separated by a hyphen.
• * - The asterisk operator means any value or always. If you have the asterisk symbol in the
Hour field, it means the task will be performed each hour.
• , - The comma operator allows you to specify a list of values for repetition. For example, if
you have 1,3,5 in the Hour field, the task will run at 1 am, 3 am and 5 am.
• - - The hyphen operator allows you to specify a range of values. If you have 1-5 in the Day
of week field, the task will run every weekday (From Monday to Friday).
• / - The slash operator allows you to specify values that will be repeated over a certain
interval between them. For example, if you have */4 in the Hour field, it means the action
will be performed every four hours. It is same as specifying 0,4,8,12,16,20. Instead of
asterisk before the slash operator, you can also use a range of values, 1-30/10 means the
same as 1,11,21.
// AAI • Cloud Technologies (CAI2C04) TEMASEK POLYTECHNIC I School of Informatics & IT
System-wide Crontab Files
• There are several special Cron schedule macros used to specify common intervals. You can use
these shortcuts in place of the five-column date specification.
• For example, use the following to restart “my_script” after each reboot:
@reboot /var/tmp/my_script
crontab -i Remove your current crontab file with a prompt before removal.
crontab -u <username> Edit other user crontab file. This option requires system administrator
privileges.
• Below are some cron job examples that show how to schedule a task
to run on different time periods.
• Run a command at 15:00 on every day from Monday through Friday:
0 15 * * 1-5 command
• Run a script every day, every hour, on the hour, from 8 AM through 4
PM:
00 08-16 * * * /path/to/script.sh
0 7 1-7 * 1 /path/to/script.sh
• Run the a script at 9:15pm, on the 1st and 15th of every month:
15 9 1,15 * * /path/to/script.sh
• TutorialsPoint
• Scheduling Cron Jobs with Crontab