GUIDE 09 / AUTOMATION
Environment & cron
Understand startup files, PATH, parent shells, scheduled jobs, and local mail.
In this guide
Commands use class paths and example filenames. Replace placeholders and inspect the target before making changes.
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.
printf "%s\n" "$PATH"
printf "PID=%s SHLVL=%s\n" "$$" "$SHLVL"
type disk-usage.sh
command -v bashOn 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.
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.
export PATH="$HOME/scripts/shell:$PATH"
export EDITOR=vim
alias du1="du -h -d 1 | sort -h"
bash -n ~/.bashrc
source ~/.bashrcSourcing 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.
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.
crontab -l
crontab -eExample 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.
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.
0 7 * * * $HOME/scripts/shell/disk-usage.sh >> $HOME/homework/week8/disk-usage.log 2>&1
*/15 * * * * $HOME/scripts/shell/health-check.sh -qCreate 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.