Linux cron job

This pages shows how to add a cron job for current user. And also shows how to send email in Amazon Linux through sSMPT.

There are 2 ways to add job,

  • There are cron folders under /etc, such as /etc/cron.hourly, /etc/cron.daily, /etc/corn.weekly and /etc/cron.monthly. Add shell script under these folders will run hourly/daily/weekly/monthly correspondingly.
  • Create a specific schedule for particular user, we will focus on this way.

First, exectue below command, will create a new cron file for current user under /var/spool/cron, the file name is same with current username

crontab -e

Second, edit generated cron file adding below line, the sendemail job will be executed every 5 minutes,

*/5 * * * * sh /root/report/sendemail.sh

In the sendemail.sh, which send email through sSMPTP.
First, install the sSMTP,

yum install sSMTP


Second, edit /etc/ssmtp/ssmtp.conf, add below,

mailhub=smtp.gmail.com:587
UseSTARTTLS=YES
AuthUser=yourusername@gmail.com
AuthPass=yourpassword

Third, the sendemail.sh looks like,

now=$(date +%m/%d/%Y)

dir=/root/report/

echo "From: OverriddenFrom<yourusername@gmail.com>" > $dir/email.txt
echo "Subject: email title $now" >> $dir/email.txt
echo >> $dir/email.txt
echo email content >> $dir/email.txt
echo >> $dir/email.txt
echo "Generated at $(date +'%m/%d/%Y %H:%M:%S')" >> $dir/email.txt

/usr/sbin/ssmtp recipient@gmail.com < $dir/email.txt

Note, the From: must be format as OverriddenFrom<yourusername@gmail.com>, if use OverriddenFrom only, gmail seems reject it, and override it to yourusername only.

Python source file vim editing indent

When editing Python source file in vim, we may want to use tab to add indent. Below shows how to use vim autocommand to add the indent automatically for*py, *pyw, *.c and *.h files.

Change the vim setting in .vimrc, add the content in  “vimrc file for following the coding standards specified in PEP 7 & 8” to the .vimrc file.

Once done, open vim to edit a new .py file, enter tab will add 8 spaces as the indent. Above Python vimrc file do more things actually, like show red when there is a invalid indent, etc.

The indent work done in the Python vimrc file is, below line of script will set tabstop=8 for new added and existing *py, *pyw, *.c and *.h file(it doesn’t touch other files other than these 4 files)

au BufRead,BufNewFile *py,*pyw,*.c,*.h set tabstop=8
au BufRead,BufNewFile *.py,*.pyw set expandtab

Details of autocommand can be found at,
autocommand tabstop
autocommand expandtab

And, there is another page show helpful info about Vi Improved, and the vim syntax config.

However, there is some confusions here, Python prefer 4 spaces for the indent, so the correct & convenient indent way is in the description here. Basically, if the tabstop is 8, set softtabstop and shiftwidth to 4, and set use noexpandtab.

tabstop is used for how to display tab(how many spaces counts for).
softtabstop is used for how many spaces will be perform when tab key entered in insert mode.
expandtab, when it is set, there will be no tab character in the insert mode, all are spaces, if softtabstop set, softtabstop spaces will be inserted, otherwise tabstop spaces will be inserted.