How do I stop a script running in UNIX?

How do I stop a script running in UNIX?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How do I stop a script running in UNIX?

You have launched mytestscript.php or any long-running process from the terminal, and you need to terminate it immediately. The instinctive reaction is often to hit Ctrl+C blindly, but that approach relies heavily on luck regarding how the underlying process handles signals. To manage this effectively, you must understand the relationship between your shell session, the operating system's job control, and the specific signal requirements of the target application.

Terminating Processes Linked to Your Session

The most common method involves sending a SIGINT (Interrupt Signal) via Ctrl+C. However, this only works if the process is a "foreground child" of your current terminal session. When you run a PHP script directly in the shell, it inherits the process group of your terminal. Sending Ctrl+C sends an interrupt signal to the entire foreground job group.

If your script is running directly in the active terminal, pressing Ctrl+C sends SIGINT (signal 2) to the PHP interpreter. In most cases, this causes the script to exit gracefully or trigger a SIGINT exception within the code if it has one defined. If the script simply ignores the signal or loops infinitely without checking $signal, you might find that Ctrl+C does nothing visible until the process eventually times out or crashes due to resource exhaustion.

Detached Processes and Job Control

The situation changes when you detach a process from the terminal using & or tools like nohup. If you run:

php mytestscript.php &

The process moves to the background. While it is still technically associated with your shell, relying on Ctrl+C here is unreliable. The signal might get lost if the shell exits, or the background process might not propagate the interrupt effectively depending on how it was spawned.

To regain control over a detached process, you must use job control commands like jobs and fg. Once brought back to the foreground with fg %1, Ctrl+C will work again because the process is once more the active child of the terminal. However, if you have used nohup or disown, the process has been decoupled from the terminal session entirely. In these scenarios, standard keyboard interrupts will fail because there is no longer an interactive terminal pipe to deliver the signal to.

Using Signal IDs for Precision

When job control is insufficient or when you know the Process ID (PID) but not the job status, sending a specific signal via kill is the precise method. The command syntax is:

kill -SIGINT <PID>

You can find the PID of your running script using ps aux | grep mytestscript.php. If the process ignores SIGINT (which happens frequently in infinite loops or poorly written scripts), you can escalate the signal severity. Sending SIGTERM (signal 15) is the standard "polite" kill command that gives the application time to clean up resources before exiting.

If even SIGTERM fails, and the script must be destroyed immediately, use kill -9 <PID>. This sends SIGKILL (signal 9), which cannot be caught or ignored by the process. The operating system forcibly terminates the memory space allocated to that PID. While effective, this should be a last resort as it leaves no opportunity for the application to flush buffers, close database connections, or save state.

Practical Considerations for PHP Scripts

PHP scripts often wrap their logic in infinite loops or event-driven architectures where they do not check for standard interruption signals without explicit code. If your script is part of a Laravel application running as a standalone CLI process, you might encounter similar issues. The underlying Swoole extension or specific worker configurations can alter how signals are handled compared to the standard PHP-FPM or CLI setups.

For production environments where scripts must run continuously but need remote management, relying on manual terminal interaction is inefficient. It is better practice to build a graceful shutdown mechanism into the script itself. This involves adding a check for $signal = SIGINT or $signal = SIGTERM at the entry point of your logic, allowing the application to perform cleanup tasks before exiting.

If you are managing multiple instances of such scripts across a server, tools like systemd offer superior control. By defining a service unit file with KillMode=process and configuring appropriate TimeoutStopSec, you can automate the termination sequence without needing to manually track PIDs or risk orphaned processes consuming system memory.

Our network