Monday, September 12, 2011

Ubuntu: How To Launch Programs In The Background From A Terminal Window

When running programs from an terminal window in Ubuntu, the program process will run in the foreground, occupying the terminal session that started the program. Until the program ends or is closed, you can not start other applications as long as this process is running in the foreground. While you can open another terminal window or use the screen command, Bash shell provides an easier function known as job control that will allow multiple programs to run in the background and return control to the terminal window on Ubuntu. 

To use job control and allow programs to return control back to the terminal window, commands entered should be followed with the ampersand character &  

For example, you want to open Firefox. From the command line you would type:

firefox &
Firefox will open in the background and the terminal window control will be returned allowing you to continue working in the terminal window. If you want to open another program or run a script, you can enter the command follow by &
For longer running programs started in the background, which do not need user input, job control commands can be executed that can show status, suspend, bring a program to the foreground, and return to background.
In the screen shot below, Firefox and Terminal server client applications were started in the background. Executing the jobs command displays all running jobs started in the background. 

ubuntujobs1.png
To bring the first program started in the background – Firefox, to the foreground and then suspend the process, execute 

fg %1
followed by Ctrl+Z

ubuntujobs2.png

To resume Firefox in the background, execute bg %1

ubuntujobs3.png

Below are all options that can be used with job control:

jobs [-lnprs] [jobspec]
jobs -x command [arguments]

The first form lists the active jobs. The options have the following meanings:

-l
List process IDs in addition to the normal information.
-n
Display information only about jobs that have changed status since the user was last notified of their status.
-p
List only the process ID of the job's process group leader.
-r
Restrict output to running jobs.
-s
Restrict output to stopped jobs.
If jobspec is given, output is restricted to information about that job. If jobspec is not supplied, the status of all jobs is listed.
If the `-x' option is supplied, jobs replaces any jobspec found in command or arguments with the corresponding process group ID, and executes command, passing it arguments, returning its exit status.
bg [jobspec]

Resume the suspended job jobspec in the background, as if it had been started with `&'. If jobspec is not supplied, the current job is used. The return status is zero unless it is run when job control is not enabled, or, when run with job control enabled, if jobspec was not found or jobspec specifies a job that was started without job control.

fg [jobspec]
Resume the job jobspec in the foreground and make it the current job. If jobspec is not supplied, the current job is used. The return status is that of the command placed into the foreground, or non-zero if run when job control is disabled or, when run with job control enabled, jobspec does not specify a valid job or jobspec specifies a job that was started without job control.
Ctrl +Z Suspend (stop, but not quit) a process running in the foreground (suspend).
Ctrl +C Interrupt (terminate and quit) a process running in the foreground.

Ubuntu: Manage time settings in ubuntu server

Manage Time in Ubuntu Through Command Line

What if you would like to manage your computer’s time in Ubuntu? It’s easy if you are in a graphical desktop environment. But what if you are on the command line? For example, in Ubuntu Server? Well, it is easy as well. A very helpful, everything-in-one-place resource is Ubuntu Time.

View Time

To view the current date and time, the following command will be enough
date

Set Time

To change time means to set a new time. To set time in Ubuntu (or any Linux), just run the following command

sudo date newdatetimestring

where newdatetimestring has to follow the format nnddhhmmyyyy.ss which is described below
  • nn is a two digit month, between 01 to 12
  • dd is a two digit day, between 01 and 31, with the regular rules for days according to month and year applying
  • hh is two digit hour, using the 24-hour period so it is between 00 and 23
  • mm is two digit minute, between 00 and 59
  • yyyy is the year; it can be two digit or four digit: your choice. I prefer to use four digit years whenever I can for better clarity and less confusion
  • ss is two digit seconds. Notice the period ‘.’ before the ss.
Let’s say you want to set your computer’s new time to December 6, 2007, 22:43:55, then you would use:

sudo date 120622432007.55

It couldn’t be any easier, could it? The source of this information was a good post on Ubuntu Forums (Set time/date via command line).

Change Time Zone

You may update or change your time zone by

tzconfig

dpkg-reconfigure tzdata (thanks to Mario, see comment below)
This command will guide you through the process of setting a new time zone. You may also choose UTC (GMT) if you want.

If your system does not have tzconfig, you may use something else.
tzselect

If your system does not have tzdata, install it as below:
sudo aptitude install tzdata

This will provide a set of different time zones to choose. If you would like to set the time to UTC, choose the option which says something like ‘none of the above’, or ‘none of these’ or something to this effect. In my case it was option 11. Then it asks for difference from UTC (GMT and GST is also the same thing). I chose GST-0 as the option and it set the time as UTC.

Sync Clock Via NTP

If you want to sync your clock with NTP servers, it is also very easy. Just make sure you have the file ntp.conf file in /etc. How can you check it?

ls /etc/ntp.conf

If you see /etc/ntp.conf as a result, you already have that file. If the ls command gives an error, you do not have it. If so, you may create it yourself.

sudo vim /etc/ntp.conf

This file will be used to automatic synchronization of the clock. I do not know if the client uses this file automatically or one has to configure something first. Thanks to Sean (see comment below): You need to install ntpd in order to make use of this ntp.conf file.

Whether you have the file already or not, make sure it has at least the following data

driftfile /var/lib/ntp/ntp.drift
server 0.pool.ntp.org
server 1.pool.ntp.org
server 2.pool.ntp.org
server pool.ntp.org


Here you may replace, add, and/or remove any servers you wish. You will find a list of time servers from the public NTP time server list.

You may manually sync the clock using the following

sudo ntpdate servername

where servername can be any public or private time server. You may always choose the following without hesitation

sudo ntpdate pool.ntp.org

If you don’t have ntpdate installed, you can install it via:

sudo aptitude install ntpdate

See, it was quite easy. Enabling NTP Services helped me gain this knowledge.