Intro To 'timeout' Command In Linux
2023-11-01 - By Robert Elder
I use the 'timeout' command, to 'Time Out' a command if it's been running for too long:
timeout 123 ./a.out
Using 'timeout' to 'Time Out' A Process
Here, I have simple program in the file 'busy-loop-program.c':
int main(){
while(1){}
return 0;
}
If I compile and run this program by itself, it will literally run forever:
gcc busy-loop-program.c -o busy-loop-program
./busy-loop-program
But if I run this program using the 'timeout' command with a value of 3, the busy loop program will be terminated after 3 seconds:
timeout 3 ./busy-loop-program
Example Use Case Of 'timeout' Command
Here's an example of a use case for the 'timeout' command that involves synchronizing backups that only transfer during the night time. Here, I have an rsync command that copies local files to a remote backup server:
/usr/bin/rsync -avvv --progress --append-verify /my-important-files/ remote-backup-server:/backups/my-important-files
If the files are very large, this could run for multiple days and slow down my network traffic for the entire duration. I can use the 'timeout' command, with a timeout value of 2 hours:
timeout 2h /usr/bin/rsync -avvv --progress --append-verify /my-important-files/ remote-backup-server:/backups/my-important-files
Then, I can install the full command as a cron job that runs every morning at 3am:
1 3 * * * timeout 2h /usr/bin/rsync -a --append-verify /my-important-files/ remote-backup-server:/backups/my-important-files
This way, the backup operation will make a bit of progress every day, and use all of the network capacity between 3 and 5 am. During the day, I'll have all the network capacity to myself.
And that's why the 'timeout' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|