LINUX - Jobs - Intro

JOBS & NOHUP & DTACH
#####################

JOBS
####

PUT JOB IN BACKGROUND - STILL TIED TO SHELL:
# command &

LIST ALL JOBS:
# jobs
# jobs -l
JOBS LISTING jobs -l PROVIDES MORE INFRO LIKE THE MUCH NEEDED PID
JOBS OUTPUT PUTS A + ON CURRENT JOB, AND - ON PREVIOUS JOB

PAUSING A PROCESS - TURNS IT INTO A PAUSED JOB - TECHINCALLY CALLED A "STOPPED" JOB:
Control+Z WHILE A PROGRAM IS RUNNING

RESTARTING PAUSED JOB:
# jobs
# fg
# %1
# %n
WHERE n IS THE JOB NUMBER THAT CAN BE SEEN FROM RUNNING "jobs". "fg" RESTARTS THE "CURRENT JOB" TO THE FOREGROUND FROM THE BACKGROUND(SO THE LAST JOB THAT WAS RAN), USUALLY I USE FG TO PUT LAST PAUSED JOB TO THE FOREGROUND IT ALSO RESTARTS PAUSED/"STOPPED" JOBS.

RESTARTING PAUSED JOB AND PUTTING INTO BACKGROUND:
# jobs
# bg
# %1 &
# %n &
# bg n
WHERE n IS THE JOB NUMBER THAT CAN BE SEEN FROM RUNNING "jobs".
WITHOUT THE n VARIABLE "bg" PUTS A JOB FROM "Stopped" STATUS TO "Running"
PICKEL: IF YOUR CAUGHT IN THE PICKLE (BAD SITUATION) WHERE YOUR JOB IS OUTPUTTING WAY TO MUCH GARBAGE TO THE FRONT EVEN THOUGH YOU JUST BACKGROUNDED IT AND RESUMED IT, LETS SAY YOUR TYPING AT THE SHELL BUT THE PROGRAM OUTPUT IS INTERFERING TO THE POINT WHERE YOU CANT TELL WHAT YOUR TYPING, THE REMEDY/SOLUTION: Control+Z MAYBE STOP THE PROGRAM ALL THE WAY AND RETHINK YOUR METHODS.

CANCEL A PROCESS - CANT BE RESTARTED - ENDS IT FOR GOOD:
Control+C

NOHUP
#####

I ALWAYS RUN NOHUP WITH &. THEY GO BEST TOGETHER AND NEVER SHOULD BE LEFT OUT. NOHUP IS MADE FOR BACKGROUNDED TASKS TO NOT DIE WHEN THE SHELL DIES, AND & PUTS TASKS IN THE BACKGROUND, THUS NOHUP CANT HAPPEN WITH OUT &.

# nohup command1 &

NOTE IF THE PROGRAM HAS SOME OUTPUT IT WILL PUT IT IN nohup.out INTO THE DIRECTORY FROM WHICH THE nohup COMMAND WAS RAN. 
TO VIEW OUTPUT 2 METHODS BELOW DEPENDING ON OUTPUT QUANTITY.

IF ALOT OF OUTPUT THATS SCROLLING FOR DAYS:
tail -f nohup.out
OR
watch -n1 "tail -n20 nohup.out"

IF NOT ALOT OF OUTPUT:
tail -f nohup.out
OR
watch -n1 cat nohup.out

SIDENOTE ABOUT TAIL AND WATCH:
TAILS "tail -f" COMMAND WAITS FOR WRITES AND OUTPUTS, WHERE AS "watch" IS INSTRUCTED TO DO A SPECIFIC FUNCTION AND REPEAT IT ON AN INTERVAL, SO WE JUST TELL IT TO "cat" OR "tail" EVERY -n# INTERVAL (IN SECONDS)
WHERE -n FOR WATCH IS THE INTERVAL IN SECONDS FOR HOW LONG TO WAIT AFTER THE COMMAND IS COMPLETE TO RUN IT AGAIN (IT TAKES DECIMAL SECONDS 0.5 IS HALF OF A SECOND)
WHERE -n FOR THE TAIL COMMAND IS THE NUMBER OF LINES OF OUTPUT

TO KILL NOHUPPED COMMAND:
# kill -9 PID
OR LESS ATTACKING:
# kill PID

REDIRECT NOHUP OUTPUT - DONT WANT IT TO SAVE TO "nohup.out" - WANT SOME OTHER LOCATION?
TO OUTPUT THE ERROR AND REGULAR OUTPUT TO DIFFERENT LOCATIONS:
nohup command1 > commandoutput.txt 2> commanderroroutput.txt
TO OUTPUT BOTH OF THE OUTPUTS TO THE SAME LOCATION:
nohup command1 > commandbothoutput.txt 2>&1
OR IN NEW BASH COMBINE THE TWO LIKE THIS
nohup command1 &> commandbothoutput.txt

DTACH - PROGRAM FOR BACKGROUND WORK:
####################################

dtach - this starts a bash/sh session in another process so if you exit out of your shell it still runs
Quick lesson on dtach:
To start a new sesssion type the following (note it makes a socket file called dtach.session):  "dtach -A /tmp/dtach.session /bin/bash"
CONTROL+\ (key above enter key) = this gets you out of your dtach session and back in the shell you originally were in
You can now close out of your shell (putty, or xterm, etc.) and it will still run
To get back in your session "dtach -A /tmp/dtach.session"
NOTE1: It doesnt matter if you call it dtach.session, i could of just called it sally without the .dtach. the /bin/bash argument is needed but you can put another shell like 
/bin/sh or even another program like rtorrent

2 OTHER PROGRAMS FOR BACKGROUND WORK
####################################
      
2. screen - this is like dtach but with more "dtachs" you can even split the screen horizontaly and verticaly
            Google it to find out how to use it - just google "how to install screen linux" and "screen linux cheatsheet"

3. byobu - this is like screen but with a cool looking bash screen border and everything and easier to use keys
            Google it to find out how to use it - just google "how to install byobu" and "how to use byobu"

READ ON MOVE PROCESS TO BACKGROUND
##################################

Comments