Mikey’s Linux Field NotesAll topics

GUIDE 09 / AUTOMATION

Environment & cron

Understand startup files, PATH, parent shells, scheduled jobs, and local mail.

4 quick lessons · Examples to adapt to your server
In this guide
Inspect the current shell environmentCustomize an interactive shellSchedule explicit commandsKeep routine runs quiet

Commands use class paths and example filenames. Replace placeholders and inspect the target before making changes.

01

Inspect the current shell environment

PATH is a colon-separated search list checked in order when a command has no slash. Exported variables are inherited by child processes. A child cannot update its parent’s variables.

COMMANDS & EXAMPLES
printf "%s\n" "$PATH"
printf "PID=%s SHLVL=%s\n" "$$" "$SHLVL"
type disk-usage.sh
command -v bash
Field note

On RHEL, ~/.bash_profile normally sources ~/.bashrc for a login Bash shell. Interactive non-login Bash loads ~/.bashrc. Keep startup files free of unsolicited output so remote commands work reliably.

02

Customize an interactive shell

Add your script directory to PATH and define short aliases in the appropriate startup file. Check syntax, then source it to update the current shell.

COMMANDS & EXAMPLES
export PATH="$HOME/scripts/shell:$PATH"
export EDITOR=vim
alias du1="du -h -d 1 | sort -h"
bash -n ~/.bashrc
source ~/.bashrc
Field note

Sourcing runs code in the current shell. Only source files you trust. A function or alias defined in a startup file is not automatically available in cron.

03

Schedule explicit commands

A user crontab has five time fields: minute, hour, day of month, month, and day of week, followed by a command. Edit your own crontab without sudo.

COMMANDS & EXAMPLES
crontab -l
crontab -e
Field note

Example crontab entries appear below. Cron uses a limited environment and does not normally load your interactive .bashrc. Use complete paths and verify the server’s time zone.

04

Keep routine runs quiet

These are crontab entries, not shell commands. The daily job appends output to a file; the 15-minute job prints only failures so cron can mail them locally.

CRONTAB
0 7 * * * $HOME/scripts/shell/disk-usage.sh >> $HOME/homework/week8/disk-usage.log 2>&1
*/15 * * * * $HOME/scripts/shell/health-check.sh -q
Field note

Create the scripts and log directory first. Cron normally mails output, not an exit status by itself. Inspect /var/log/cron with sudo and read local mail with mailx. The mail service must be configured and running.