Running script at boot

Hi everyone,

I just got an android phone that I will want to interface with my linux box, which is currently behind a time warner cable modem. In the morning when I’m groggy, and my computer is off, I want to be able to kick the power button on, leave for work, and have it e-mail me the ip address I get from time warner, so I can check it on my phone.

I don’t have the script written or anything yet, all I’m asking about here is how I would go about getting the script to run once I have it – I can boot to a user using an automatic login and probably get away with it but that seems to be poor practice.

I’m really a stranger to the world of customizing a bootup, a pointer to a practical guide would be appreciated.

Leaving the computer on all the time is a bad option even though my electric is included in rent, I live in a studio and I don’t like the noise, and the environment and all. And manually doing mindless, automatic work is not the reason I have computer.

Thanks for any help,
Malcolm

A simple solution would be to run the script from crontab if you know roughly when the machine is on. For example, if you know your machine is already at 8:00am, then just edit your crontab, and add a line like this:

0 8 * * * your_script

that way, your don’t need to log in.

Hope that works.

PS. If you have to run at boot, just install a bootup manager and you should be able to figure how to add your script as a service. But this is not a critical service, so don’t need to run from root, just from security point of view.
Cheers.

What you want is to register for a dynamic DNS service like dyndns and run a service on your computer like inadyn that will update the DDNS record. This will work better than email because you can use the domain name as normal and it will continue to work even when TW changes your IP address at DHCP lease renewal.

So, if you are DIRECTLY connected to the cable modem, you can get the IP from a script, but you need to be root. Here is an example:

#!/bin/bash

#: Title       : /home/james/bin/readip
#: Date Created: Tue Oct 12 21:38:40 CDT 2010
#: Last Edit   : Tue Oct 12 21:38:40 CDT 2010
#: Author      : nixCraft,J. McDaniel
#: Version     : 1.00
#: Description : 
#: Options     : 

# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------

# Get OS name

if  $UID -ne 0 ]]; then
  echo "Root User Permissions are required, Please Enter the ..."
  echo
  sudo $0 
  exit 0
fi

OS=`uname`
IO="" # store IP
case $OS in
   Linux) IP=`ifconfig  | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}'`;;
   FreeBSD|OpenBSD) IP=`ifconfig  | grep -E 'inet.[0-9]' | grep -v '127.0.0.1' | awk '{ print $2}'` ;;
   SunOS) IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2} '` ;;
   *) IP="Unknown";;
esac
echo "$IP"

exit 0
# End Of Script

Copy and paste this text into the script called readip into your home area bin folder (~/bin/readip). Once the file has been saved, run the following command to make it executable:

chmod *x ~/bin/readip

There is a mailx command that can be ran from the command prompt, but you must configure it first. And, your PC would need to auto Log in. You sure you can’t do some of this manually before you leave?

Thank You,

On Wed, 13 Oct 2010 02:36:02 +0000, malcolm81 wrote:

> Hi everyone,
>
> I just got an android phone that I will want to interface with my linux
> box, which is currently behind a time warner cable modem. In the
> morning when I’m groggy, and my computer is off, I want to be able to
> kick the power button on, leave for work, and have it e-mail me the ip
> address I get from time warner, so I can check it on my phone.
>
> I don’t have the script written or anything yet, all I’m asking about
> here is how I would go about getting the script to run once I have it –
> I can boot to a user using an automatic login and probably get away with
> it but that seems to be poor practice.
>
> I’m really a stranger to the world of customizing a bootup, a pointer to
> a practical guide would be appreciated.

I do something like this on my system at home - twice a day the following
command (would be easy to turn into a script) runs via a cron job to e-
mail me my IP addresses:

/usr/bin/dig +noall +answer @207.69.188.185 dnsname1.org dnsname2.org
dnsname3.org | awk ‘{print $1 " " $5}’ | mail -s “IP Address Change” -r
from@dnsname1.org root myemail@mail.com

(That sends to both root on the system it runs from and
myemail@mail.com) The output format is:

dnsname1.org 1.2.3.4
dnsname2.org 5.6.7.8
dnsname3.org 9.1.2.3

And that’s what’s in the e-mail I receive. You could put something like
this in /etc/rc.d/boot.local (or equivalent) so it runs at startup. I
combine this with a dyndns account (which is where the DNS names are
updated) and my router updates those when the address changes.

Jim


Jim Henderson
openSUSE Forums Administrator
Forum Use Terms & Conditions at http://tinyurl.com/openSUSE-T-C

On 2010-10-13 05:06, smjohn3 wrote:
>
> A simple solution would be to run the script from crontab if you know
> roughly when the machine is on. For example, if you know your machine is
> already at 8:00am, then just edit your crontab, and add a line like
> this:
>
> 0 8 * * * your_script

Manual page crontab(5)

These special time specification “nicknames” are supported, which replace the 5 initial time
and date fields,
and are prefixed by the ‘@’ character:
@reboot : Run once, at startup.
@yearly : Run once a year, ie. “0 0 1 1 *”.
@annually : Run once a year, ie. “0 0 1 1 *”.
@monthly : Run once a month, ie. “0 0 1 * *”.
@weekly : Run once a week, ie. “0 0 * * 0”.
@daily : Run once a day, ie. “0 0 * * *”.
@hourly : Run once an hour, ie. “0 * * * *”.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

On 2010-10-13 04:36, malcolm81 wrote:

> I don’t have the script written or anything yet, all I’m asking about
> here is how I would go about getting the script to run once I have it –
> I can boot to a user using an automatic login and probably get away with
> it but that seems to be poor practice.

Cron - see man 5 crontab, search word “boot”. Easiest one, I think. Can run as user.

Or use an init script - see an example in /etc/init.d/skeleton, or “man init.d”. There is also a
chapter on this on the suse book. Don’t use boot.local, network is down at that moment.

Or put a script in “/etc/sysconfig/network/if-up.d/”, where it will run whenever network goes up.
This would be the “proper” way.

Or track the router log (if possible), to see when it changes IP, if it does.

Or use a dynamic dns service. Good method, except that it does not say if machine is up.


Cheers / Saludos,

Carlos E. R.
(from 11.2 x86_64 “Emerald” at Telcontar)

With the email method you only know that it was up at the time the email went out. Anyway a Linux machine should have good uptime, but the network in between, who knows.

Wow, great replies!

Figured it out piece meal from the help above, and I am definitely going to look into setting up a dynamic dns service because that would make it easy to use from the phone end, but for now I am just happy to get the ip in the mail.

I ended up using the script from jdmcdaniel3, as named readip, and using that as the start of the pipe in the mail command from hendersj, and sticking the following script into /etc/sysconfig/network/if-up.d/ as suggested by Carlos E. R.:


#!/bin/bash

echo `/home/malcolm/bin/readip` | mail -s "Your New IP" -r malcolm my.personal.email@wee.fun


Then I just had to flip a few settings to keep it out of my spam folder and I am good to go!

Thanks everybody,
Malcolm

malcolm81, happy to hear you go it going as you wished. Good luck with your setup.

Thank You,

malcolm81, you might want to check out the script by please_try_again called blip, in my #23 message there. It will get your internet IP address, which should be the same as you get from the first script in your case.

netinfo - Read Network & PC Information into a Local Text File

For anyone that is using a residential router, the IP address you get from the first script is from the router and not the internet, while this second script would always be the internet IP. Just for your information.

Thank You,

You’ve made it a bit harder on yourself than need be:

/home/malcolm/bin/readip | mail -s "Your New IP" -r malcolm my.personal.email@wee.fun

No sense in quoting and unquoting the script output.