Coding⏱️ 2 min read📅 2026-05-31

How to Fix: How to kill a child process after a given timeout in Bash?

Automate process termination with timeout in bash.

Quick Answer: Use `timeout` command to execute a command with a specified timeout, e.g., `timeout 60s /path/to/your/process

To fix the issue of killing a child process after a given timeout in Bash, you can use the `timeout` command. This command allows you to set a time limit for a command or process and will terminate it if it exceeds that time.

How to Use Timeout

  • Use the `timeout` command followed by the desired timeout time in seconds, e.g., `timeout 60 bash -c 'your_command_here'

For example, if you want to run a child process for 1 minute and kill it if it doesn't return within that time, you can use the following command:

timeout 60 bash -c 'your_command_here'

Using "nohup" and Timeout

  • Another approach is to use the `nohup` command in combination with `timeout`. The `nohup` command allows a process to continue running even after the user logs out, and `timeout` will terminate it if it exceeds the specified time.

For instance, you can use the following command:

nohup timeout 60 bash -c 'your_command_here'

Final Words

By using the `timeout` command, you can effectively kill a child process after a given timeout in Bash and prevent it from hanging indefinitely.

Did this fix your problem?

If not, try searching for specific error codes.

🔍 Search Error Database

❓ Frequently Asked Questions