Author Topic: Run script once at startup  (Read 764 times)

0 Members and 1 Guest are viewing this topic.

Offline DaveWellman

  • Jr. Member
  • **
  • Posts: 3
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Run script once at startup
« on: January 25, 2013, 08:25:14 am »
Hi,
This is possibly an easy thing to do but I'm fairly new to Linux so I'm not quite sure.

I need to run a shell script once when the system starts. How do I get that to happen?

I initially thought of using cron, but that seems to be for repeated tasks. So I then looked into using an entry in /etc/init.d/rc.5 (as an example) but those seem to be for 'services' which would seem to be over the top for what I want.

Is there a more commonly used option or is one of these the best way forward ?

I can't put this into any logon processing for a user because the system in question is a server and hence on most times no user will logon.

Many thanks,
Dave

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Run script once at startup
« Reply #1 on: January 25, 2013, 03:57:37 pm »
Hi, three possibilities;

a. Add your command to /etc/rc.local - this is the easiest
b. Write your own init script and install in init.d, then link to /etc/init.d/rc<n>.d (on ubuntu, use "update-rc.d")
c. Add to cron, with "crontab -e" (as root) then add "@reboot  /<your script>" at the top, no time information

..
« Last Edit: January 25, 2013, 04:25:21 pm by Mad Penguin »

Offline Mad Penguin

  • #Mad_Penguin_UK
  • Administrator
  • Hero Member
  • *****
  • Posts: 1320
  • Karma: 10017
  • Gender: Male
    • View Profile
    • Linux in the UK
    • Awards
Re: Run script once at startup
« Reply #2 on: January 25, 2013, 03:58:33 pm »
(/etc/rc.local might be /etc/init.d/rc.local depending on your distro)

Offline Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13988
  • Karma: 347
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Run script once at startup
« Reply #3 on: January 25, 2013, 04:19:28 pm »
It would help if you said which Linux distribution/version ;)
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline DaveWellman

  • Jr. Member
  • **
  • Posts: 3
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Run script once at startup
« Reply #4 on: January 26, 2013, 09:55:46 am »
Hi Mark,

Many thanks for that, I used the "cron @reboot" option and it works a treat! My distro is SUSE v10.

Another question if I may.

In a script, how do I set the 'current directory' to be the one where the script is located? The situation I've got is as follows:

- I use cron to run scripts as required
- near the start of most of these I have a "cd" command to set the current directory. This allows my code which accesses files in the script directory to work fine.
- as soon as I move the scripts or send them to a different machine (where I have no control over the directory names) then this is one thing that has to be changed.
- I'm assuming that there will be a commend (or two) that I can use to "set current directory to thw one containing the script file".

All ideas  gratefully received.

Cheers,
Dave

Offline Mark Greaves (PCNetSpec)

  • Administrator
  • Hero Member
  • *****
  • Posts: 13988
  • Karma: 347
  • Gender: Male
  • "-rw-rw-rw-" .. The Number Of The Beast
    • View Profile
    • PCNetSpec
    • Awards
Re: Run script once at startup
« Reply #5 on: January 26, 2013, 10:12:47 am »
Wouldn't
Code: [Select]
cd ./
do the trick ?
WARNING: You are logged into reality as 'root'

logging in as 'insane' is the only safe option.

Offline DaveWellman

  • Jr. Member
  • **
  • Posts: 3
  • Karma: 0
  • I've just joined!
    • View Profile
    • Awards
Re: Run script once at startup
« Reply #6 on: January 26, 2013, 10:45:07 am »
Don't know, I'll try it.

Thanks.

Offline SeZo

  • Hero Member
  • *****
  • Posts: 1524
  • Karma: 120
  • Gender: Male
    • View Profile
    • Awards
Re: Run script once at startup
« Reply #7 on: January 28, 2013, 12:18:58 am »
Quote
In a script, how do I set the 'current directory' to be the one where the script is located?

How about BASH_SOURCE[0]?
You could try (untested):
Code: [Select]
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
« Last Edit: January 28, 2013, 12:39:07 am by SeZo »

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal