Pause a running script in Mac terminal and then resume later

Mac OSX 24 ก.พ. 2020

If the script is running in the foreground of your terminal, you can press Control-Z to pause the script. This will suspend the running of the script.

To restart it, type jobs and you'll see the suspended job listed there. Type fg or more specific fg %x where x is the number of the suspended job.

$ test.pl   # Test script (prints out Foo every two seconds
Foo!
Foo!
^Z
$  # Job has been suspended
$ jobs
[1] + Stopped                  ./test.pl
$ fg %1  #Restarts Job #1
Foo!

The Control-Z key that suspends the job is the default, but could be modified. The stty can change this and will show you the current default:

$ stty -a     
speed 9600 baud; 40 rows; 120 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^H; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;

If you're using BASH as the shell (which is the default shell on a Mac), you can use BASH's built in job control capabilities.

If the script is running in the foreground of your terminal, you can press Control-Z to pause the script. This will suspend the running of the script.

To restart it, type jobs and you'll see the suspended job listed there. Type fg or more specific fg %x where x is the number of the suspended job.

$ test.pl   # Test script (prints out Foo every two seconds
Foo!
Foo!
^Z
$  # Job has been suspended
$ jobs
[1] + Stopped                  ./test.pl
$ fg %1  #Restarts Job #1
Foo!

The Control-Z key that suspends the job is the default, but could be modified. The stty can change this and will show you the current default:

$ stty -a     
speed 9600 baud; 40 rows; 120 columns;
lflags: icanon isig iexten echo echoe -echok echoke -echonl echoctl
        -echoprt -altwerase -noflsh -tostop -flusho pendin -nokerninfo
        -extproc
iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel iutf8
        -ignbrk brkint -inpck -ignpar -parmrk
oflags: opost onlcr -oxtabs -onocr -onlret
cflags: cread cs8 -parenb -parodd hupcl -clocal -cstopb -crtscts -dsrflow
        -dtrflow -mdmbuf
cchars: discard = ^O; dsusp = ^Y; eof = ^D; eol = <undef>;
        eol2 = <undef>; erase = ^H; intr = ^C; kill = ^U; lnext = ^V;
        min = 1; quit = ^\; reprint = ^R; start = ^Q; status = ^T;
        stop = ^S; susp = ^Z; time = 0; werase = ^W;

You can see the very last line has susp = ^Z. This is the key that will suspend your script. In this case, it's Control-Z.

You can also use the bg command to make a suspended job run in the background. However, that background job will terminate when you close the shell/Terminal Window unless you had prepended nohup to the front of the command.

Pause a running script in Mac terminal and then resume later
How can I pause (not stop) a running script from Terminal in OSX, to resume it later from the point it paused?

💪🏻⚡️

แท็ก

Onyx

Just a middle-aged programmer, Can do many things but not the most.