LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2010, 10:53 AM   #1
Daggaroth
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Rep: Reputation: 0
Need to create a shell script that would automatically backup and shutdown


I would like to develop a shell script that would automatically run a backup program and then shutdown the computers, but I do not know the first thing about script development or even writing scripting. if anyone could help me out I would be greatly appreciated.
 
Old 11-29-2010, 11:14 AM   #2
b0uncer
LQ Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
A script, at its shortest, is simply a series of commands that your shell understands (as if you typed them in yourself). To shut down your computer (from a terminal) you'd use something like shutdown,

Code:
man shutdown
A bash script, for example, could be simply

Code:
#!/bin/bash
shutdown
and after

Code:
chmod +x script.sh
you could run it (provided that the file was called script.sh and that shutdown was in a directory that was included in $PATH environment variable). You should probably put the whole path to shutdown into the script, to be sure. The first row in the script tells which interpreter should be used to run the script, in this case bash (there are lots of other options here, depending on how you want to do things, bash is just one way). Or, you could just put the word "shutdown" in the file (call it, say, "scriptfile") and run

Code:
sh scriptfile
and you'd very probably get the same result--here you'd explicitly make "sh" (which might actually be "bash") run the contents (or "script") of the given file. You wouldn't even have to make it executable using "chmod" if you did it this way. This way you can just add programs to be run into the script, like your backup program (whatever it is), and run the script. Or you could run it through cron for example, to make it all happen at some pre-specified time (just add the script to crontab, and make sure that you specify all executables with full path, to make sure everything works).

That's the basics. There are loads of tutorials out there about scripting on a variety of languages, so keep reading if you just got interested. After you get past the plain ol' running commands state, you'll soon learn you can do a bunch of things. Actually, your whole backup program can very well be "simply" a script, even though it did wonders. And yes, at some point you do want to know how to run your scripts at startup (init scripts) or at a given time (cron/anacron) or at shutdown or at the end of the day or at tea time or whenever you didn't know you wanted them to run..so get used to crons and such The first problems you'll probably face when timing the running of your scripts, i.e. not running them yourself directly, are "program not found"s or equivalents, which are caused by you not telling exactly what to run and where from. As long as you work in your nice comfortable personal shell with a fancy environment defined, all works well, but you can't rely on all those environment variables etc. being set all the time (such as $HOME or $PATH -- try to echo them to see what they contain!).

Last edited by b0uncer; 11-29-2010 at 11:24 AM.
 
Old 12-03-2010, 01:26 AM   #3
Daggaroth
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
so if I wanted my script to do what I want it to do the entirety would be:

"
#!/bin/bash
chmod +x script.sh
run <insert backup program here>
if
command completed
Program < insert backup Program here> completed
then
Shutdown
sh scriptfile

"

would that work or am i missing something that would screw it up?

please keep in mind this is my very first time writing a script so I am not sure if it would work or not.

Last edited by Daggaroth; 12-03-2010 at 01:27 AM.
 
Old 12-03-2010, 01:51 AM   #4
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
No, there are a number of issues with your "script".

Firstly, what you have is pseudo code at the moment, which is fine as a starting point to structure your thoughts and decide what needs to be done, and in what order, I normally put in comments for this:

Code:
#!/bin/bash
###
### script to run a backup and then shutdown the computer
###
LOGFILE="/backup.log"
echo "Starting backup script $(date)" > $LOGFILE
###
### First I need to run the backup, but to where and using what program?
###
### There are multiple backup utilities including (but not limited to): tar, cpio, amanda, backup 
###
echo "Run backup script here..." >> $LOGFILE 2>&1

###
### Next I need to check the return code of the backup utility
###
if [ "$?" -eq "0" ]
then
   ###
   ### Utility worked - shutdown machine!
   ###
   echo "Shuting down the machine" >> $LOGFILE
else
   ###
   ### Utility failed! - ABORT
   ###
   echo "Backup FAILED! - Read the logfile $LOGFILE"
   exit 1
fi
Secondly, the chmod +x command in your post needs to be run on the script not from within the script.

Thirdly, there is no point in running a script post the shutdown command, as once this completes the script will have nothing to run against.

Hope this helps
 
Old 12-03-2010, 02:08 AM   #5
Daggaroth
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Ahhh! I see now,

thank you!
 
Old 12-03-2010, 02:37 AM   #6
Daggaroth
LQ Newbie
 
Registered: Nov 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Disillusionist View Post


if [ "$?" -eq "0" ]

what does this specific aspect of the code do? Im not familiar with it. well specifically the aspect in the brackets
 
Old 12-03-2010, 11:19 AM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
When you run any command, the exit code is recorded in the special variable $?

An exit code of zero means that the previous command exited cleanly, normally meaning that the task worked.

It is important to note that this is the result of the previous command, so do not put any echo commands in between or this will ruin your test

The square brackets denote a test, so in pseudo code this would be:

Code:
##
## Test exit code of previous command.
##
## If this was zero the command worked :)
##
## Otherwise the command failed :(
##
In my section for a failure, I am using exit passing an exit code of 1, therefore you could perform a similar test on the script itself (although once this is configured to shutdown the machine on success this would seem a little self evident )
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to automatically login through ssh and create user Sayan Acharjee Linux - General 10 10-21-2010 01:51 AM
Autorun Backup script before shutdown!!!!! jp286 Linux - Networking 1 11-25-2008 10:29 AM
how to create backup MYSQL Script to backup my database for every 1hour RMLinux Linux - Newbie 3 11-20-2008 10:13 AM
How to create/delete temp/backup files through a shell script ? Sid2007 Programming 4 10-17-2007 01:55 PM
Automatically running script at shutdown samac Slackware 2 05-14-2005 10:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration