SysAdmin‎ > ‎

Cron and Bash Script

Setting up Cron Jobs and Bash Scripting

For more info you can look at the sites I used or Google cron jobs and bash scripting separately

https://help.ubuntu.com/community/Beginners/BashScripting

https://help.ubuntu.com/community/CronHowto

http://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

http://www.marksanborn.net/linux/learning-cron-by-example/

http://askubuntu.com/questions/536766/how-to-make-crontab-email-me-with-output

http://www.nixtutor.com/linux/sending-email-alerts-through-cron/

http://www.nixtutor.com/linux/send-mail-with-gmail-and-ssmtp/


To make or edit a crontab, type in

crontab -e

Or to edit crontab with gedit instead of the terminal editor type in

EDITOR=”gedit” crontab -e

At the end of the file after all the commented out documentation you can add a new job

Format is as following, minutes hours dayofmonth month dayofweek command

For the time and date you can enter a specific time or date with numbers, and for command you type in what you want the job to do, command must be full path

It is better to read up on a better guide on one of the links provided or google it


To remove crontab for user type in

crontab -r

To see your crontab type in

crontab -l

There is also a system crontab located in /etc/crontab and has a slightly different format

minutes hours dayofmonth month dayofweek user command

user specifies which user should run that command


You can set up crontab to email you output by adding in above your jobs

MAILTO=email@domain.com

Read up on the links provided to see how to set up email, the way I did it was install sSMTP and set that up with an email to use, read up on the nixtutor links for guide on how to set it up


You can use cronjobs to open a bash script file

The command should just open the bash script like this, /home/user/Desktop/bashscriptfile

To make a bash script just make a new file, edit it with vi or gedit and add in

#!/bin/bash


list of commands

List of commands can be whatever you want, it can be changing directories, making new files, using rsync to do backups, etc

You don't need to save it as a file extension

To run a command with a certain user you can use

su user -c “command”

You have to change permissions of the bash script file to be executable, type in

chmod 700 /home/user/Desktop/bashscriptfile

700 makes it read write and executable only by the user

You can run bash script files by either typing in

./bashscriptfile

If you are in the same directory as where it is located or use the full path

/home/user/Desktop/bashscriptfile


The next step you should do after asking Professor Gallicchio

To login to Maratea without password you need to setup a ssh keygen on Policoro by typing in

ssh-keygen -t rsa

Go to the .ssh folder by typing in

cd .ssh

Copy the content of the id_rsa.pub file

Login to Maratea and make a folder called .ssh

Change to that directory and make a file called authorized_keys

Paste in the content of your id_rsa.pub file into the authorized_keys file and save it

Now log out of Maratea and try logging back in to see whether or not it worked, it shouldn't ask you for a password



Comments