Timing commands in linux

Display Date And Time In Linux

H ow do I display date and time in Linux using the command line and GUI options?

To display date and time under Linux operating system using command prompt use the date command. It can also display the current time / date in the given FORMAT. We can set the system date and time as root user too.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux with the terminal application
Est. reading time 3 minutes

Syntax

Open a terminal and type the following command:
date

You can format the date as follows in dd-mm-yy format:
date +»%d-%m-%y»

Simply display the current time:
date «+%T»

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

To print the date of the day before yesterday, run:
date —date=’2 days ago’
Want to see the day of year of Christmas in the current year? Try:
date —date=’25 Dec’ +%j
Display the current full month name and the day of the month:
date ‘+%B %d’

Display date and time in Linux using the timedatectl

Most modern Linux distro with systemd use the timedatectl command. It is used to query and change the system clock and its settings, and enable or disable time synchronization services. All you have to do is type the following command:
$ timedatectl

TZ environment variable

The TZ environment tells Linux what time zone you are in. Many times when you want to change your time zone temporarily. For example, you might want to print the current date and time in “America/Los_Angeles” timezone even though you are in “Europe/London”. So we can set TZ, give a command like as follows when using TCSH/CSH:
setenv TZ timezone
For BASH/KSH/SH (see export command:
TZ=timezone; export TZ
Another option:
TZ=»America/Los_Angeles» date
TZ=»Asia/Tokyo» date
Use the following command to print a list of all timezones:
timedatectl list-timezones
timedatectl list-timezones | more
## filter out data using the grep command/egrep command ##
timedatectl list-timezones | grep -i Hong_kong
timedatectl list-timezones | grep -i paris
timedatectl list-timezones | grep -E -i ‘paris|london|kolkata’

The TZ Environment Variable on Linux

GUI Tool: Time Administration

The Time Administration Tool allows you to set the time, date and timezone of your system, as well as setting any time server to synchronize your local time server. Type the following command to start time admin tool:
sudo time-admin
## OR ##
time-admin

Fig.01: Linux Date and Time Administration Tool

How to change date and time settings with Gnome based Linux desktop

First, you need to Settings in Activities and then click Details in the sidebar. Make sure you click Date & Time in the sidebar to open the panel:

Conclusion

In this quick tutorial, you learned about the date command that you can use to see or change the date/time under Linux operating systems. We further explained how to use the GUI tools too. The date command has many more options. See man page by typing the following man command:
$ man date

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

How to Run a Command with Time Limit (Timeout) In Linux

Linux comes with a bounty of commands, each command unique and used in specific cases. The goal of Linux is to help you be as fast and efficient as possible. One property of a Linux command is the time limit. You can set a time limit for any command you want. If the time expires, the command stops executing.

In this short tutorial, you are going to learn two methods on how you can use a time limit in your commands.

On this page:

Run Linux Commands Using the timeout Tool

Linux has a command-line utility called a timeout, which enables you to execute a command with a time limit.

Its syntax is as follows.

To use the command, you specify a timeout value (in seconds) with the command you want to run. For instance, to timeout a ping command after 5 seconds, you can run the following command.

You do not have to specify the (s) after number 5. The command below is the same and will still work.

Timeout Ping Command in Linux

Other suffixes include:

  • m representing minutes
  • h representing hours
  • d representing days

Sometimes commands may continue to run even after timeout sends the initial signal. In such instances, you can use the —kill-after option.

Here’s the syntax.

You need to specify a duration to let timeout know after how much time the kill signal is to be sent.

For example, the command shown is going to be terminated after 8 seconds.

Set Time Limit to Linux Commands

Run Linux Commands Using Timelimit Program

The Timelimit program runs a given command then terminates the process after a specified time using a given signal. It initially passes a warning signal, and then after a timeout, it sends the kill signal.

Unlike the timeout option, Timelimit has more options such as killsig, warnsig, killtime, and warntime.

Timelimit can be found in the repositories of Debian-based systems and to install it, use the following command.

For Arch-based systems, you can install it using AUR helper programs e.g., Pacaur Pacman, and Packer.

Other Linux distributions, you can download timelimit source and manually install it.

After installation, run the following command and specify the time. In this example, you can use 10 seconds.

Note that if you don’t specify arguments, Timelimit uses the default values: warntime=3600 seconds, warnsig=15, killtime=120, and killsig=9.

Conclusion

In this guide, you have learned how to run commands with a time limit in Linux. In review, you can use the Timeout command or the Timelimit utility.

The Timeout command is easy to use, but the Timelimit utility is a bit complicated but has more options. You can choose the most suitable option depending on your needs.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How to Time a Command in Linux

If you want to be a good Linux administrator, therefore you should know time command. It is used to determine how long a given command takes to run.

It is useful for testing the performance of your scripts and commands (ie helps to find execution time for shell scripts or time taken for a command to finish).

How to Use Linux time command

To use time command, just execute time with the command/program you want to run as input. Please check below example

The real signifies the wall clock time the ‘ping’ command took from execution till termination, user and sys are the time taken by ping the user space and kernel space.

How to Make time command writes its output to a file

To write the time command output to a file instead of the print out screen, use the -o command line option, which expects a file name/path as input.

Now we will display ping’s output on stdout, while the time command output will be written to the text file.

Note: We used /usr/bin/time instead of time because the shell built-in time command doesn’t offer the -o option.

How to appends its output to an existent file

To append the time command output to an existent file instead of overwrite it, use the -a command line option.

How to get Detailed output of linux time command

We can use the -v command line option to produce detailed output.

How to customizing time command output

There are a large number of formatting options as shown in the following list

C — Name and command line arguments used
D — Average size of the process’s unshared data area in kilobytes
E — Elapsed time in a clock format
F — Number of page faults
I — Number of file system inputs by the process
K — Average total memory use of the process in kilobytes
M — Maximum resident set the size of the process during the lifetime in Kilobytes
O — Number of file system outputs by the process
P — Percentage of CPU that the job received
R — Number of minor or recoverable page faults
S — Total number of CPU seconds used by the system in kernel mode
U — Total number of CPU seconds used by user mode
W — Number of times the process was swapped out of main memory
X — Average amount of shared text in the process
Z — System’s page size in kilobytes
c — Number of times the process was context-switched
e — Elapsed real time used by the process in seconds
k — Number of signals delivered to the process
p — Average unshared stack size of the process in kilobytes
r — Number of socket messages received by the process
s — Number of socket messages sent by the process
t — Average resident set size of the process in kilobytes
w — Number of time the process was context-switched voluntarily
x — Exit status of the command

We can use the formatting switches as follows:

The output for the above command would be something like this:

If we want to add a new line as part of the format string use the newline character as follows:

Linux Time Command Versions

There are three-time command Versions, Bash, Zsh and Gnu time command. We can use the type command to determine whether time is a binary or a built-in keyword.

Please write your suggestions or comments on time command and for more information use time man pages.

Источник

Put a Timer on Your Running Commands With Timeout Command in Linux

With the timeout command you can set a time limit on running other commands and programs. If the program runs longer than the set limit, timeout kills it.

Sounds good, right? Let’s see how to use this command.

Using timeout command in Linux

Here’s the syntax for the timeout command:

As you can see, the time is always set in seconds here.

Let’s take an example with the sleep command. The sleep command basically ‘sleeps’ till the allotted time. Which means it just waits for that many seconds.

So if I use sleep 10, the shell waits for 10 seconds. But if I put a timeout of 4 seconds, the sleep command that was supposed to run for 10 seconds, ends in just 4 seconds.

By default, the timeout command sends SIGTERM to politely kill the running program. You may send some other signal using the -s option.

You can see all the kill signals in Linux with kill -l command. Let’s say you want to send the SIGKILL signal (kill -9) instead of the default, SIGTERM. You can use something like this:

You could also use KILL or 9 (the number for SIGKILL as listed in kill -l) instead of SIGKILL.

Hard killing a program is not a nice thing to do. But a program can ignore the SIGTERM.

A better way to handle is to send the kill signal only when your initial termination request is ignored by the running program.

So, let’s say you want to run a program not more than 5 seconds. But if the running program refuses to stop, you send the kill signal 3 seconds after sending the default SIGTERM. Here’s what you can use:

This means your program won’t run more than 8 seconds. You can use the time command to check how long did the program actually run.

Is there a practical use of timeout command?

It sounds like one of those obscure Linux commands that you might not use every day. It could, however, be useful in some special situations.

For example, if you started playing a terminal game to fresh your mood for a bit but you lost track of the time and kept on playing far more than the desired time.

The timeout command is helpful in such cases.

Stay tuned to learn more Linux commands. Do subscribe to the newsletter to receive regular tips and tricks.

Источник

Linux Set Date and Time From a Command Prompt

H ow can I set the system date and time from the command prompt (bash shell)? I don’t have GUI installed and I am login over ssh session. How can I set date under Linux operating systems?

Use the date command to display the current date and time or set the system date / time over ssh session. You can also run the date command from X terminal as root user.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements None
Est. reading time 2m

This is useful if the Linux server time and/or date is wrong, and you need to set it to new values from the shell prompt.

You must login as root user to use date command.

Linux Display Current Date and Time

Just type the date command:
$ date
Sample outputs:

Linux Display The Hardware Clock (RTC)

Type the following hwclock command to read the Hardware Clock and display the time on screen:
# hwclock -r
OR
# hwclock —show
$ sudo hwclock —show —verbose

OR show it in Coordinated Universal time (UTC):
# hwclock —show —utc
Sample outputs:

Linux Set Date Command Example

Use the following syntax to set new data and time:
date —set=»STRING»
For example, set new data to 2 Oct 2006 18:00:00, type the following command as root user:
# date -s «2 OCT 2006 18:00:00»
OR
# date —set=»2 OCT 2006 18:00:00″
You can also simplify format using following syntax:
# date +%Y%m%d -s «20081128»

Linux Set Time Examples

To set time use the following syntax:
# date +%T -s «10:13:13»
Where,

Use %p locale’s equivalent of either AM or PM, enter:
# date +%T%p -s «6:10:30AM»
# date +%T%p -s «12:10:30PM»

How do I set the Hardware Clock to the current System Time?

Use the following syntax:
# hwclock —systohc
OR
# hwclock -w

A note about systemd based Linux system

With systemd based system you need to use the timedatectl command to set or view the current date and time. Most modern distro such as RHEL/CentOS v.7.x+, Fedora Linux, Debian, Ubuntu, Arch Linux and other systemd based system need to the timedatectl utility. Please note that the above command should work on modern system too.

timedatectl: Display the current date and time

Type the following command:
$ timedatectl

Fig.01: Systemd Linux timedatecetl command to display the current date and time

How do I change the current date using the timedatectl command?

To change the current date, type the following command as root user:
# timedatectl set-time YYYY-MM-DD
OR
$ sudo timedatectl set-time YYYY-MM-DD
For example set the current date to 2015-12-01 (1st, Dec, 2015):
# timedatectl set-time ‘2015-12-01’
# timedatectl
Sample outputs:

To change both the date and time, use the following syntax:
# timedatectl set-time YYYY-MM-DD HH:MM:SS
Where,

  1. HH : An hour.
  2. MM : A minute.
  3. SS : A second, all typed in two-digit form.
  4. YYYY: A four-digit year.
  5. MM : A two-digit month.
  6. DD: A two-digit day of the month.

For example, set the date ’23rd Nov 2015′ and time to ‘8:10:40 am’, enter:
# timedatectl set-time ‘2015-11-23 08:10:40’
# date

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

How do I set the current time only?

The syntax is:
# timedatectl set-time HH:MM:SS
# timedatectl set-time ’10:42:43′
# date
Sample outputs:

How do I set the time zone using timedatectl command?

To see list all available time zones, enter:
$ timedatectl list-timezones
$ timedatectl list-timezones | more
$ timedatectl list-timezones | grep -i asia
$ timedatectl list-timezones | grep America/New
To set the time zone to ‘Asia/Kolkata’, enter:
# timedatectl set-timezone ‘Asia/Kolkata’
Verify it:
# timedatectl

How do I synchronizing the system clock with a remote server using NTP?

Simply type the following command:
# timedatectl set-ntp yes
Verify it:
$ timedatectl
Sample outputs:

Conclusion

Linux users can use date command to print or set the system date and time. Systemd based Linux users can use timedatectl to control the system time and date.

  • You can also set new timzone using this mini-howto.
  • Man pages – timedatectl(8)

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Читайте также:  Trendnet tu s9 linux driver
Оцените статью