Upgrade to Pro — share decks privately, control downloads, hide ads and more …

BASH for Control Freaks

BASH for Control Freaks

Basic of job control in BASH: Background, foreground, list, and manage running jobs.

Steven Lembark

October 12, 2024
Tweet

More Decks by Steven Lembark

Other Decks in Technology

Transcript

  1. Bourne Again BASH v5 is a general-purpose tool. Today we

    will look at: Job Control: Running background jobs.
  2. Control Freaks Welcome BASH processes can be managed as “jobs”.

    Processes can be Put in the background. Brought to the foreground.
  3. Control Freaks Welcome BASH processes can be managed as “jobs”.

    Processes can be Put in the background. Brought to the foreground. Stopped & Started.
  4. Control Freaks Welcome BASH processes can be managed as “jobs”.

    Processes can be Put in the background. Brought to the foreground. Stopped & Started. Listed & Tracked.
  5. Control Freaks Welcome man 1 bash; /JOB CONTROL JOB CONTROL

    Job control refers to the ability to selectively stop (suspend) the execution of processes and continue (resume) their execution at a later point. A user typically employs this facility via an interactive interface supplied jointly by the operating system kernel’s terminal driver and bash.
  6. Step 1: Backgrounding You have one session available. And two

    big tables to restore from external tables in s3. $ psql > select count(1) from foo_s3archive; count ----------- 1000000000 (1 row) > select count(1) from bar_s3archive; count ----------- 2000000000 (1 row)
  7. Step 1: Backgrounding You have one session available. And two

    big tables to restore from s3. Q:How can we restore them in parallel? $ psql > select count(1) from foo_s3archive; count ----------- 1000000000 (1 row) > select count(1) from bar_s3archive; count ----------- 2000000000 (1 row)
  8. Step 1: Backgrounding You have one session available. And two

    big tables to restore from s3. Q:How can we restore them in parallel? A:By running two copies of psql. $ psql > select count(1) from foo_s3archive; count ----------- 1000000000 (1 row) > select count(1) from bar_s3archive; count ----------- 2000000000 (1 row)
  9. Step 1: Backgrounding Safe copies take two steps. $ cat

    recover_foo.psql; create table foo_tmp like foo; insert into foo_tmp ( select * from foo_external ) ; insert into foo ( select * from foo_tmp ); drop table foo_tmp;
  10. Step 1: Backgrounding Start by creating the destination tables. *_tmp

    tables avoids corruption if the extenal connecion has issues. $ cat recover_foo.psql; create table foo_tmp like foo; insert into foo_tmp ( select * from foo_external ) ; insert into foo ( select * from foo_tmp ) ; drop table foo_tmp;
  11. Step 1: Backgrounding Now run the first script. This is

    going to take a while. $ psql > \i recover_foo
  12. Step 1: Backgrounding Background the running insert. Hitting ctrl-Z (“^Z”)

    in BASH sends a SIGTSTP to the running process. The process is not dead, but also not running. $ psql > \i recover_foo ^Z [1]+ Stopped psql
  13. Step 1: Backgrounding You can see the backgrounded process with

    “jobs”. This shows that job #1 is stopped. Not running, but also not dead. $ jobs [1]+ Stopped psql
  14. Step 1: Backgrounding Now background the job with “bg”. This

    shows job #1 is running ‘&’. It also gives us a prompt. $ jobs [1]+ Stopped psql $ bg [1]+ psql & $
  15. Step 1: Backgrounding Run another psql. Background it also. Job

    #1 is still running. Job #2 is ready to be started in the background. $ psql > \i recover_bar ^Z [2]+ Stopped psql $ jobs [1]- Running psql & [2]+ Stopped psql
  16. Step 1: Backgrounding Run another psql. Background it also. Job

    #1 is still running. Job #2 is ready to be started in the background. $ psql > \i recover_bar ^Z [2]+ Stopped psql $ jobs [1]- Running psql & [2]+ Stopped psql $ bg [2]+ psql &
  17. Step 1: Backgrounding Viola! Two running psql jobs! Catch: You

    don’t know what they are doing. $ jobs [1]- Running psql & [2]+ Running psql &
  18. Step 1: Backgrounding Better way: \o redirects output. Allows a

    tail to check the query results. $ psql > \o /var/tmp/recover_foo.out > \i recover_foo.psql ^Z [1]+ Stopped psql $ bg [1]+ psql & $ psql > \o /var/tmp/recover_bar.out > \i recover_bar.psql ^Z [2]+ Stopped psql $ bg [2]+ psql & $ tail -f \ /var/tmp/recover-{foo,bar}.out;
  19. Step 2: Foregrounding Say job #1 finishes... Use job control

    to foreground psql. The ‘%’ prefix tells bash to work on a job number. Hit Enter for a prompt. $ tail -f ... INSERT 0 1000000000 $ jobs [1]- Running psql & [2]+ Running psql & $ fg %1 psql >
  20. Step 2: Foregrounding You can start another psql job. Or

    just exit. $ tail -f ... ... INSERT 0 2000000000 $ jobs [1]- Running psql & [2]+ Running psql & $ fg %1 psql > exit
  21. Step 2: Foregrounding This leaves job #2 still running. Notice:

    The job number doesn’t change! Even if %1 exits, the one running job is still “%2”. $ jobs [2]+ Running psql &
  22. bg and SIGHUP Background jobs ignore HUPS. If you loose

    your connection the job keeps running. Catch: It lacks a terminal. $ jobs [2]+ Running psql &
  23. bg and SIGHUP Background jobs ignore HUPS. If you loose

    your connection the job keeps running. Catch: It lacks a terminal. Your new process has no jobs. $ jobs
  24. bg and SIGHUP Background jobs ignore HUPS. If you loose

    your connection jobs usually keep running. Note: Systems may deliver more than SIGHUP to kill orphaned proc’s! $ jobs
  25. bg and SIGHUP Background jobs ignore HUPS. If you loose

    your connection jobs usually keep running. Catch: They have no controlling termainal! $ jobs
  26. bg and SIGHUP Few fixes if you get disconnected: ”reptr”

    is a utility for re- attaching to terminals.
  27. bg and SIGHUP Few fixes if you get disconnected: End

    your SQL script with an “exit”. Eventually psql exits on its own.
  28. bg and SIGHUP Few fixes if you get disconnected: Put

    “\q” into the buffer before backgrounding the job. This will cause an exit once the “\i” completes.
  29. bg and SIGHUP Few fixes if you get disconnected: psql,

    like most programs, will exit gracefully on a TERM. Use “pkill -TERM psql” to locate the job and stop it that way. $ pgrep -a psql; 54741 psql $ kill -TERM 54741; # or just $ pkill -TERM psql;
  30. %1 %ce %?ce %1, %2... list jobs by number. %XX

    uses the job name. %?XX looks for ‘XX’ anywhere in the command. $ psql … ^Z $ fg %vs; # psql somescript & $ fg %?somescript;
  31. Shortcuts Foreground doesn’t require “fg”, just the ‘%’ specifier. “%1”

    is job #1. “%ps” is a job running ”psql”. “%?somescript” searches the entire commandl line. $ psql … ^Z # these both work $ %1 $ %ps # psql somescript & $ %?somescript;
  32. Shortcuts %% is the last stopped job. %+ is the

    last backgrounded job. $ psql … ^Z # fg the last job stopped $ fg %% # psql somescript & $ %?somescript;
  33. Bedside reading As always: man bash; Learning the bash Shell,

    3rd Edition https://www.oreilly.com/library/view/learning-the-bash/0596009658/ Lots of alternatives online: https://duckduckgo.com/?q=learning+bash+shell&t=ffab&ia=web