Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

Tuesday, November 22, 2011

Write Startup scripts in Linux Environment?

Explanation is based on SLES (SUSE Enterprise Linux). It could be related to other distributions of Linux as well.
There are various situations where you write your own script and want it to run at system startup. In Linux environment you have to place your script in /etc/init.d and use 'insserv' command or 'chkconfig' config to insert those script in various runlevels.

[By default, insserv insert the script in runlevel 3 and 5. You can check that by running #chkconfig --list ]

Let's say I want to run 'asterisk' program automatically at startup and I want to make sure that it starts only when all the services of OS are up.

  • 'which' command returns me the location of the program or application#which asterisk
/usr/bin/asterisk

  • Create a script file in /etc/init.d directory
#vi /etc/init.d/asteriskStartupScript
#!/bin/bash
#Copyright (c) 2011 DShah
# All rights reserved
#
#Author: DShah, 2011
# /etc/init.d/asterisk
#PLEASE READ /etc/init.d/skeleton to understand various parameters in startup scripts
#
### BEGIN INIT INFO
# Provides: asterisk
# Required-Start: $ALL
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Starts Asterisk
### END INIT INFO

#Start service
/usr/sbin/asterisk

Save and close the script file.

  • 'chmod' command can be use to add executable permission
#chmod 744 /etc/init.d/aseriskStartupScript

  • 'insserv' command can be used to insert the script in desired runlevel as specified in script file
#insserv asteriskStartupScript

  • 'chkconfig' command can be used to check if the script will run in next startup
#chkconfig --list asteriskStartupScript


Reference:
http://www.novell.com/coolsolutions/feature/15380.html

Thursday, September 23, 2010

Rename multiple files in Linux

Lets' change all files with extension ‘.html’ to ‘.php’.

Rename all *.html files in one folder

rename

#rename .html .php *.html



Do the same operation recursively in a directory tree

find . -name "*.html" -exec rename .html .php {} \;

Those double quotes around *.info are important



If you want to be expert go to following link and don't forget to read all the comments :)
http://www.thegeekstuff.com/2009/06/how-to-rename-files-in-group/

Friday, May 28, 2010

Make copy batch file in windows

If you need to make a copy of your local drive to the backup drive scheduled everyday, you will need to know couple of things. [I am talking on Windows environment ;) ]

1. Download the robocopy. It is one of the microsoft product. It stands for robust copy

This is well built GUI tool. However to understand the various options available, you can check this
http://ss64.com/nt/robocopy.html

2. Install robocopy. You can write the simple script to make the copy process. [ You can use any kind of editor like notepad, wordpad, notepad++ to write the script]

robocopy source_folder destination_folder [file(s)_to_copy] [options]


#
# Script file name: C:\mycopy.cmd
#
@echo "Copying files and folders"
robocopy "C:\myLocalFile" "F:\myBackup" /E

@echo "Copy process completed"

/E  option allows to Copy Subfolders, including Empty Subfolders


3.
Finally you can schedule the command file(batch file)
created by you to run in the specific time.

Go to Control Panel --> Scheduled Tasks -->
Right click and create a new schedule task
-->
Give the name to it --->
Right click on the scheduled task created and go to the properties -->
specify the path of the script file in the run section under Task Tab

(in my case it would be C:\mycopy.cmd ) -->
Now go to the Schedule Tab and specify the schedule you wanted


Hey you are done.

Thursday, April 9, 2009

Shell Scripting plus AWK and SED

Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file

==========================================================
awk utility is powerful data manipulation/scripting programming language (In fact based on the C programming Language). Use awk to handle complex task such as calculation, database handling, report creation etc.

General Syntax of awk:

Syntax:awk -f {awk program file} filename

awk Program contains are something as follows:

Pattern
{
action 1
action 2
action N }

awk reads the input from given file (or from stdin also) one line at a time, then each line is compared with pattern. If pattern is match for each line then given action is taken. Pattern can be regular expressions.

======================================================

SED is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). SED works by making only one pass over the input(s), and is consequently more efficient.

General Syntax of sed
Syntax:
sed -option 'general expression' [data-file]
sed -option sed-script-file [data-file]

http://freeos.com/guides/lsst/index.html