11 ‐ Different types of Jobs - CloudScope/DevOpsWithCloudScope GitHub Wiki

fg, bg, and Jobs

fg and bg are commands used to manage jobs in the shell. A job is a process started by the shell that can run in the foreground or background.

Jobs in Shell

  1. Foreground Jobs:

    • A job running in the foreground takes control of the terminal.
    • You can't use the terminal for other commands until the job finishes.
    • Example: Running a script in the foreground:
      sleep 60
      
  2. Background Jobs:

    • Background jobs run independently of the terminal, allowing you to continue using the terminal for other commands.
    • You can start a job in the background by appending & at the end of the command.
    • Example:
      sleep 60 &
      
  3. Job Control Commands:

    • jobs: Lists all jobs with their status.
      jobs
      
    • bg: Resumes a suspended job in the background.
      • Usage: bg [job_id]
      • Example: bg %1 resumes job 1 in the background.
    • fg: Brings a job to the foreground.
      • Usage: fg [job_id]
      • Example: fg %1 brings job 1 to the foreground.
    • Ctrl+Z: Suspends a running foreground job and sends it to the background in a stopped state.
    • kill: Sends signals to jobs or processes to terminate or manage them.
      • Usage: kill %job_id or kill PID.

Cron Jobs

Cron jobs are scheduled tasks that run automatically at specified intervals using the cron daemon (crond). They are useful for automating repetitive tasks like backups, monitoring, and periodic updates.

Understanding Cron

  1. Cron Daemon:

    • crond is the background service that reads crontab files and executes scheduled tasks.
  2. Crontab:

    • A crontab file contains a list of cron jobs with their schedules.
    • Each user has their own crontab file, and there's also a system-wide crontab.
  3. Crontab Syntax:

    • A cron job consists of five time-and-date fields followed by the command to run:
      * * * * * command_to_run
      
    • Fields:
      • Minute (0-59)
      • Hour (0-23)
      • Day of the month (1-31)
      • Month (1-12)
      • Day of the week (0-7) (0 and 7 represent Sunday)
    • Example: Run a script every day at 2 AM:
      0 2 * * * /path/to/script.sh
      
  4. Managing Crontab:

    • View crontab: crontab -l
    • Edit crontab: crontab -e
    • Remove crontab: crontab -r
  5. Special Strings:

    • @reboot: Run once, at startup.
    • @hourly: Run every hour (0 * * * *).
    • @daily: Run every day at midnight (0 0 * * *).
    • @weekly: Run every week on Sunday at midnight (0 0 * * 0).
    • @monthly: Run the first day of every month at midnight (0 0 1 * *).
    • @yearly or @annually: Run once a year at midnight on January 1 (0 0 1 1 *).
  6. Logging and Troubleshooting:

    • Cron logs are usually found in /var/log/cron, /var/log/syslog, or similar, depending on the system configuration.
    • Ensure scripts have executable permissions and correct paths.

Summary

  • fg and bg: Used to manage running jobs by bringing them to the foreground or sending them to the background.
  • Jobs: Shell processes that can run in the foreground or background.
  • Cron Jobs: Automated tasks scheduled with crontab, managed by the cron daemon.