Creating and Running a Script

Script is sequence of instructions for which we have a repeated use. Using the cron facility, scripts can be used to automate the tasks. It seems difficult to start but is the simplest thing to learn. We can make our work easy with shell scripting. Here, I’m using a simple example to explain its working:-

Problem: I want to change the brightness on my screen by running a script automatically during boot.

Creating a Script:

  1. First of all, install xbacklight to manage the brightness of your screen. Use the following command:
    $ sudo apt-get install xbacklight inotify-tools
  2. Now, create a bash file using any editor with an extension of .sh and  add commands in that. Here, I made a file brightnessControl.sh
    • $ vim brightnessControl.sh
    • this
  3. Write the code as shown and save this.
  4. Now, $ chmod +x brightnessControl.sh
  5. To run this use, $ ./brightnessControl

Run a script when system boots as root:

  1. Add permissions first off.
    $ chmod 755 brightnessControl.sh
  2. Open Statrup Applications> Add new > sh /etc/init.d/brightnessControl.sh
  3. Save and close. And restart. It’s done.

Create symbolic link-

for example if you wanted to run a program in the graphical run level 2, the default run level for Ubuntu, you would place it in the /etc/rc2.d directory. You just cannot place it the directory, you must signify when it will run by indicating the startup with an “S” and the execution order is important. Place it after everything else that is in the directory by giving it a higher number.

If the last script to be run is rc.local and it is named S99rc.local then you need to add your script as S99myscript.

ln -s /etc/init.d/brightnessControl /etc/rc3.d/S99brightnessControl

Also, make sure brightnessControl file is executable and run this command-
$ sudo update-rc.d brightnessControl defaults

So, with shell scripts you can make your work easy.

Advertisements

Published by

kaurdavinder

I am the one who discover myself daily through reading, writing, interacting with people and expressing my thoughts to inspire them if I would be able to bring a little change to the society through my work. I write blogs, participate in various social activities and want to be a Professional Speaker. Currently, I am pursuing my post graduation in Computer Applications from Guru Nanak Dev Engineering College, Ludhiana and I’m very much active in many technical & social communities.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s