Tuesday, 4 April 2017

Starting a Process

1. Foreground Processes
2. Background Processes

Whenever you execute a command in UNIX, it creates a new process. 
The operating system tracks processes through a five digit ID number known as the pid or process ID. Each process in the system has a unique pid. Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any one time, no two processes with the same pid exist in the system. You can use the ps command to see what processes you are running and all processes on the system. When you start a process (run a command), there are two ways you can run it in the foreground or background. The difference is how the process interacts with you at the terminal.

Foreground Processes
By default, every process that you start runs in the foreground. It gets its input from the keyboard and sends its output to the screen. The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input, it waits for it from the keyboard. While command is running, I cannot run any other commands. For the commands, which usually runs very quickly, this is not a problem. But if there is a command that runs for a long time terminal will be tied up.

Background Processes
A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits. The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!
The simplest way to start a background process is to add an ampersand ( &) at the end of the command.

Suspending foreground processes
While a foreground process runs, the shell does not process any new commands. Before you can enter any commands, you have to suspend the foreground process to get a command prompt. The suspend key on most UNIX systems is Ctrl+Z. If Ctrl+Z does not work for you, use the stty command  to determine the key for your system.

Moving processes between the foreground and background
When a foreground process is suspended, a command prompt enables you to enter more commands; the original process is still in memory but is not getting any CPU time. To resume the foreground process, you have two options background and foreground. The bg command enables you to resume the suspended process in the background; the fg command returns it to the foreground. Put the job number on the bg or fg command, prefixed with a percent sign.

Nohup Command
You can prevent a background process from terminating, which is the default action, when you sign off or are disconnected. The nohup command prevents your process from getting the HUP (Hang UP) signal and enables it to continue processing. Add nohup command before the command you actually want to run. Because nohup is designed to run when there is no terminal attached, it wants you to redirect output to a file. If you do not, nohup redirects it automatically to a file known as nohup.out.

Wait Command
There are three ways to use the wait command with no options (the default), with a process ID, or with a job number prefixed with a percent sign. The command will wait for the completion of the job or process you specify.If you do not specify a job or process (the default ), the wait command waits for all background jobs to  finish. Using wait without any options is useful in a shell script that starts a series of background jobs.When they are all done, it can continue further processing.

Monday, 27 March 2017

chown and chgrp commands examples

The command chown, an abbreviation of change owner, is used to change the owner of file system files, directories.
Actually the chown command can be used to change both user and group ownership, while the chgrp command can only be used to change group ownership.
CHOWN syntax
chown options user:group file/folder

Example:
1. Making john as the owner of the file1:
$ chown john file1
But the group owner will not be changed.

2. Changing ownership of multiple files:
$ chown john /path/to/file1 /path/to/file2 /path/to/file3

3. Change the owner and group  for a file.
$ chown john:artist file1

4. Using chown command on symbolic link file
When the chown command is issued on symbolic link to change the owner as well as the group then its the referent of the symbolic link whose owner and group will be changed by default. 
Using flag ‘-h’, you can forcefully change the owner or group of a symbolic link.
chown -h john:artist file_symlnk

5. Change owner only if a file already owned by a existing user
$ chown --from=john smith file

6. Change only group if a file has another group already present
$ chown --from=:artist :sports file1

7. Change owner and group using UID and GID
Use those UID and GID in place of new owner and group
$ chown 459:732 file1

8. Changing group to primary group.
$ chown john: file1
By default primary group is assigned to the file1if kept blankspace after:

9. Changing only group 
$ chown :artist file1

10. Copy the owner/group from one file to another using the ‘–reference’ flag
$ chown --reference=file1 newfile

CHGRP syntax
chgrp options groupname file/folder

Example:
1. Change the group  for a file.
$ chgrp artist file1

2. Changing group of multiple files:
$ chgrp artis /path/to/file1 /path/to/file2 /path/to/file3

3. Using chgrp command on symbolic link file
chgrp -h artist file_symlnk

4. Copy the group from one file to another using the ‘–reference’ flag
$ chgrp --reference=file1 newfile

5. To print the verbose messages only when changes made in file
$ chgrp -c artist file1

-R –recursive to operate on files and directories recursively
-v –verbose to output a diagnostic for every file processed