Ubuntu - Running scripts at startup

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
Hi all, just a quick and hopefully simple problem i was wondering someone could help me with

I want to run a custom script /script/myscript.pl in the background at startup, ideally this would be done before logon so its running regardless to whether someone logs on or not.

Now my problem is that the script is required to parse system logs so never ends, when added to the init.d the script will start but the session will hang at start up as it waits for inputs from the logs rather than continuing to boot

Is there any way to start this script in the background while allowing the system to boot normally

I am a complete linux noob so step by step instructions would be much appreciated

Thanks in advance for any help
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,801
script is designed to continue running aye? I'd say wrap it from init perhaps, with a background thing of some sort.

you can test it by creating a file in the init.d that reads /path/to/myscript &
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
yeah, the script runs forever because of the following

Code:
use IO::Handle;
autoflush STDOUT;
use Fcntl qw(:seek);

if(!-r $file)
{
        die "Can not read $file !";
}

my $pos = (stat($file))[7];

while (1)
{
        if((stat($file))[7] < $pos)
        {
                print STDERR "WARNING -- logfile may have been truncated.  Resetting the position...\n";
                $pos = 0;
        }

        open LOG, $file or die "Cannot open $file, $!";

        seek LOG, $pos, SEEK_SET if defined $pos;
        while(<LOG>)
        {
          $line = $_;
          chomp($line);       #Removes new line character from entry
          process_log($line); #Call function to precess log line

        }
        $pos = tell LOG;
        close LOG;

        sleep 1;
}

how would i make it run in the background? i tried using at now but it doesnt seem to like it :S
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
oo thanks a bunch, finally getting somewhere

i know how to run it as a background app now, i missed a print statement i used for testing so it wasnt running correctly

i just do
Code:
at -q b -m now 
at> perl /scripts/myscript.pl
Now to work out how to that scriptually and add it to init.d or maybe even a cron job :s
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
ok i created a file for the init.d and put it in the /init.d folder
Then i added the shortcut
perl /scripts/myscript.pl &

will let you know how it goes
 

Chronictank

FH is my second home
Joined
Jan 21, 2004
Messages
10,133
you sir are a genius

I created a script in the /etc/init.d directory called myscript
inside it i added the line

perl /scripts/myscript.pl &

Then i added it to the init.d and rc.d

sudo chmod +x /etc/init.d/myscript

sudo update-rc.d myscript start 51 S .

And voila it worked :D
i guess i owe you a pint :)
 

GReaper

Part of the furniture
Joined
Dec 22, 2003
Messages
1,983
Erm, init scripts are usually a bit more detailed than that! :p

You should have a file called /etc/rc.local - or something similar where you can put in simple little commands for anything you've got which doesn't have an init script. It starts at boot after everything else.

Another option would be to add it to your crontab:

@reboot /usr/bin/perl /scripts/myscript.pl >/dev/null 2>&1

Which is far far easier if you're a regular user and don't want to mess about with the system config.
 

TdC

Trem's hunky sex love muffin
Joined
Dec 20, 2003
Messages
30,801
ofc they are, but we leave error checking and state changing until he needs it. screw it. it works, and I claim my pint :)
 

Users who are viewing this thread

Top Bottom