Master these methods to easily set time limits in your Linux system
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
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

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
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
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 ~]#

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!

This guide explores essential Linux tools for monitoring and troubleshooting disk I/O performance, a crucial metric impacting server speed and application responsiveness. Disk I/O performance directly affects how quickly data is read from and written

For new Linux users, identifying connected devices is crucial, especially USB drives. This guide provides several command-line methods to determine a USB device's name, essential for tasks like formatting. While USB drives often auto-mount (e.g., /

One of the most common problems with Linux systems, especially those with limited disk space, is the exhaustion of root partition (/) space. When this problem occurs, you may encounter the following error: No space left on device Don’t panic! This just means that your root directory (/partition) is full, which is a common problem, especially on systems with limited disk space or servers running 24/7. When this happens, you may encounter the following problems: The package cannot be installed or upgraded. System startup failed. The service cannot be started. Unable to write to logs or temporary files. This article walks you through practical steps to identify problems, clean up space safely, and prevent them from happening again. These instructions are suitable for beginners

This article explores top-notch Notepad alternatives for Linux users. Notepad , while excellent on Windows, lacks a Linux version. This guide offers a diverse range of options to suit various needs and preferences. Top Notepad Alternatives for

Several days ago, I encountered a 32-bit CentOS 8 distribution and decided to test it on an older 32-bit system. Post-boot, I discovered a network connectivity issue; the connection would drop, requiring manual restoration after each reboot. This pr

Let's clarify what constitutes a bad sector or bad block: it's a portion of a hard drive or flash memory that's become unreadable or unwritable, typically due to physical damage to the disk surface or malfunctioning flash memory transistors. Accumul

The cp command, short for "copy," is a fundamental tool in Linux and other Unix-like systems for duplicating files and directories. While efficient for local file transfers, for network-based copies, scp (secure copy) is preferred due to i

When using the rm command to delete a file or directory in Linux system, if you encounter the following error: rm: cannot remove 'file-or-directory': Device or resource busy Don't worry, this is a common problem, which means that the file or directory you are trying to delete is currently being used by the system or running process. Cause of error The "Device or Resource Busy" message indicates that the file or directory is in use. To avoid damaging the system or causing data loss, Linux prevents deleting files in use. Common reasons include: Your terminal is currently in the directory you want to delete. The program or process is using the file or directory.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
