Home >System Tutorial >LINUX >Master these methods to easily set time limits in your Linux system

Master these methods to easily set time limits in your Linux system

WBOY
WBOYforward
2024-02-11 17:48:15747browse

As the pace of work and life accelerates, we often need to set some time limits in Linux systems to control our behavior. Whether it is to limit user login time or limit process running time, Linux systems provide a variety of methods to achieve this purpose. So, do you know how to set time limit in Linux system? This article will introduce several common methods so that you can easily master them.

timeout is a command line utility that runs a specified command and terminates it if it is still running after a given period of time. The timeout command is part of the GNU core utility package, which is installed on almost all Linux distributions

how to use

Grammar format:

timeout [OPTION] DURATION COMMAND [ARG]...

DURATION can be a positive integer or floating point number, followed by an optional suffix:

  • s – seconds (default)
  • m – minutes
  • h – hours
  • d – day

If no unit is added, the default is seconds. If DURATION is 0, the associated timeout is disabled.

Example

Terminate the ping operation after 5 seconds:

[root@localhost ~]# timeout 5 ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=16.3 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.0 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.0 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=17.6 ms

Master these methods to easily set time limits in your Linux system
Terminate ping operation after 5 minutes:

[root@localhost ~]# timeout 5m ping www.baidu.com

Terminate ping operation after 1 day:

[root@localhost ~]# timeout 1d ping www.baidu.com

Terminate the ping operation after 2.5 seconds:

[root@localhost ~]# timeout 2.5s ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=14.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=15.6 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=15.6 ms
Master these methods to easily set time limits in your Linux system

Send the specified signal

If no signal is given, timeout sends a SIGTERM signal to the managed command when the time limit is reached. The signal to be sent can be specified using the -s (-signal) option.

Send SIGKILL signal to ping command, terminate after 5 seconds:

[root@localhost ~]# sudo timeout -s SIGKILL 5s ping www.baidu.com
PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=1 ttl=55 time=17.2 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=2 ttl=55 time=16.6 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=3 ttl=55 time=16.7 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=4 ttl=55 time=16.2 ms
64 bytes from 61.135.169.125 (61.135.169.125): icmp_seq=5 ttl=55 time=16.7 ms
Killed

Master these methods to easily set time limits in your Linux system
A signal can be assigned its name or its sequence number. The SIGKILL serial number used below will terminate the operation after 5 seconds:

[root@localhost ~]# sudo timeout -s 9 5s ping www.baidu.com
PING www.a.shifen.com (61.135.169.121) 56(84) bytes of data.
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=1 ttl=55 time=15.5 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=2 ttl=55 time=16.3 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=3 ttl=55 time=14.9 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=4 ttl=55 time=16.0 ms
64 bytes from 61.135.169.121 (61.135.169.121): icmp_seq=5 ttl=55 time=22.0 ms
Killed

Master these methods to easily set time limits in your Linux system
To see all available signals, use the kill -l command to view all signals.

[root@localhost ~]# kill -l
 1) SIGHUP  2) SIGINT  3) SIGQUIT  4) SIGILL  5) SIGTRAP
 6) SIGABRT  7) SIGBUS  8) SIGFPE  9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX 
[root@localhost ~]#
Master these methods to easily set time limits in your Linux system

Stop the stuck process

SIGTERM, the default signal sent when the time limit is exceeded, can be caught or ignored by some processes. In this case, the process continues running after sending the termination signal.

To ensure that the executed command is terminated, please use the -k (-kill after) option followed by a time. It will be forced to end when the given time limit is reached.

In the following example, the timeout command runs for one minute, and if the command does not end, the command will be terminated after 10 seconds:

[root@localhost ~]# timeout -k 10s 1m sh test.sh

Run in the foreground

By default, timeout runs managed commands in the background. If you want to run the command in the foreground, use the --foreground option:

[root@localhost ~]# timeout --foreground 5m ./script.sh

Summarize

This article introduces several common methods of setting time limits in Linux systems, including using the ulimit command, using the pam_time module, using cron scheduled tasks, etc. By understanding these methods, you can better control user behavior, optimize system performance, prevent resource waste, etc. I hope readers can choose a method that suits them based on actual needs and apply it.

The above is the detailed content of Master these methods to easily set time limits in your Linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete