Startup Scripts

From DD-WRT Wiki

Revision as of 14:42, 4 September 2006 by AlReece45 (Talk | contribs)
Jump to: navigation, search

Contents

Introduction

Sometimes you install things and want them to load on startup. Writing startup scripts is the way to go!

Web Interface Method

Prerequisites

  • Know how to use and access Web Interface
  • Know the sequence of commands you'd like to run every startup
  • (optional) test commands at the command line to ensure correct operation

Instructions

  1. Using the Web Interface, goto the Administration tab.
  2. If available, go to the Command subtab, otherwise go to the diagnostics subtab.
  3. Type the commands you wish to run every startup into the Commands: dialog box (place each command on a newline using the enter key)
  4. Click the Save Startup button at the bottom of the page if you'd like the commands to save into the rc_startup variable (you may need to scroll). Save Firewall will save the commands into the rc_firewall variable.

How it Works

What this does is save your string of commands to the rc_startup variable in flash memory (also known as an NVRAM variable). Since flash memory doesn't get erased on startup, your "startup script" will remain with every boot and will always execute.

NVRAM Method

Prerequisites

  • Know how to access and use Telnet or SSH
  • Know the command(s) you wouldd like to run every startup
  • (optional) Test commands for correct operation.

Instructions

  1. Login to your router using Telnet/SSH
  2. To set the startup scripts, use the following:
 nvram set rc_startup="
 <command 1>
 <command 2>
 ...
 <command n>
 "
 nvram commit

A good trick for entering long scripts that, say you downloaded from the internet, is the Paste function of Windows Telnet.

At the telnet prompt enter:

nvram set rc_startup="

When it switches to the > prompt, copy the text from notepad or your web browser, or wherever your script is saved, then right-click the telnet logo at the top of the telnet window, and point to Edit, then click Paste. Voila! the entire script is typed out, with each command neatly placed on its own line. Now type the final " and hit enter to save.

How it Works

When you issue the first command you'll notice the prompt change to a ">" From here on every line you type is added to the rc_startup variable. You are still technically typing on the same command line, even though you are using multiple lines When you type the final '"' and hit [Enter] you'll see the prompt change back and your first command has been executed. That is, your script has been added to the variable rc_startup.

At this point you can check the contents of rc_startup by issuing the command:

~ # nvram get rc_startup

When you give the command nvram commit the rc_startup variable is saved in flash memory and will remain when you reboot.

Shell Script Method (Unfinished)

AFTER WRITING THIS WIKI I found that this method DOES NOT WORK Only 1 of the startup scripts runs. I attribute the failure this The last step This can be solved by writing a script that launches all of the other scripts and setting that to load on rc_starup but I'm not sure how to reliably write such a script. The *.startup method does not, however, work. If someone can repair this, please help. EDIT: This has been repaired. The startup script works now.

I assume this is because the shell will only take the first match from *.startup ie a.startup or whatever it picks first (this is not always alphabetical). As you say, the correct way to do this is to create a shell script that calls other shell scripts as required.

Simple nvram scripts are fine for short scripts, but if you'd like to do a lot, or if you'd like to easily manage your startup programs, the best method is through using shell scripts.

Shell scripts make it easier to write long scripts that wget files from the internet, or ipkg -d ram install, copy pre made configuration files from the flash partion, etc. before finally running your program, and do so on every startup.

This means you don't need to save so much stuff in router flash space (rather limited) but can fill up the much more prevalent RAM without having to install and configure your program on every reboot.

And, since every program gets it's own script, removing a program from the next boot is as easy as deleting, moving, or renaming it's startup script file.

Prerequisites

-Knowledge of Telnet/SSH and the Command Line
-know a command or sequence of commands you'd like to run every startup
-it's best if you've tested your commands to ensure accuracy
-Have SCP or another working method to copy files to/from the router
-you have jffs initialized and working

Windows users
-TextPad or Win32Pad
(or other *nix friendly text editor. DO NOT USE NOTEPAD)

Instructions

Write the Shell Script

Write a shell script for each program you would like to run on startup.
your script should be a *.startup file

Shell scripts should look like this:

#!/bin/sh
<command 1>
<command 2>
<command 3>
...
killall <command n program name>
<command n>

Because of something in the linux system, your shell script could end up being run periodically, and not just at boot (cron maybe??). I'm not a linux user, so I'm not entirely sure why. However, this does mean that you'll need to ensure that only 1 copy of your program is ever run at a time. The killall command ensures that <command n> isn't running more than once simultaneously. This assumes that all commands in the script above <command n> exit immediately after executing (such as cp, wget, or iptables.)

Ex a script: kismet_server.startup

#!/bin/sh
# Make the /tmp/usr/bin folder and move there
mkdir /tmp/usr
mkdir /tmp/usr/bin
cd /tmp/usr/bin

#copy the executable file from my home webserver
wget http://192.168.1.2/kismet_server

#kill an open copies of kismet_server
killall -q kismet_server

#execute kismet_server using /jffs/etc/kismet.conf as the configuration file.
/tmp/usr/bin/kismet_server -n -f /jffs/etc/kismet.conf

Save the Script

Save the script on the router in the /jffs/etc/config directory. You may need to create this directory. Do so either in winscp or using the mkdir command.

Make the Script Executable

Making your script executable means changing the file rights to "700" Do this through winscp or with the command

~ # chmod 700 /jffs/etc/config/<scriptname>.startup

Tell your scripts to load on startup, ipup or wanup

AFAIK, you should be able to reboot the router at this point and everything should be OK. Something is supposed to launch these scripts at router boot. This has not been the case for me and I've found solice in setting an rc_startup script that launch any/all scripts found in the /jffs/etc/config directory.

Do so with the following commands

~ # nvram set rc_startup="
  > for I in `/bin/ls /jffs/etc/config/*.startup`
  > do
  >    sh $I &
  > done
  > "
~ # nvram commit

(Edited rcalvert 2005-08-23 to process all startup scripts)

For additional startup scripts, omit this setup (4.3.4)

Additional Comments by --CaScAdE 12:22, 26 Jun 2006 (CEST)
On current versions you can leave the custom startup script away, at least works for me when using .startup files in /jffs/etc/config

As far as I know DD-WRT executes three types of files in three locations according to different circumstances: The three directories are:

  • /etc/config
  • /mmc/etc/config
  • /jffs/etc/config

and the types are:

  • .startup - will be executed on system startup, normally boot-time and: _before_ the firewall is configured
  • .if - will be executed whenever an interface goes up (ex: ppp connection is reestablished after a disconnect, goes up first time) and: _after_ firewall stuff, so good ending for custom QoS and stuff
    Does it still work?
  • .wanup - should be a better place for disconnect/reconnect events than the ones above
  • .wanup - will be executed whenever an interface goes up (ex: ppp connection is reestablished after a disconnect, goes up first time) and: _after_ firewall stuff, so good ending for custom QoS and stuff
  • .ipup is run when ppp connection is reestablished after a disconnect, _after_ firewall stuff.
  • .ipdown is run when ???

  • How it Works

    The command sh /jffs/etc/config/*.startup will execute every script ending in .startup found in the /jffs/etc/config directory. By setting this as the rc_startup value, your *.startup scripts will load every time the router boots.

    Shell scripts are much like Windows/Dos batch (*.bat) files. Every line is a new command and execution occurs exactly as if each line was typed seperately into the command line.

    Samba method

    The drawback of jffs is the fact it doesn't work on some firmwares and it is rather small. If you have a Samba or Windows server running on your LAN you could use the fabulous 'sambashare' option. This adds a vast amount of writable storage to your router, and it is possible to set an automatic startup script when the samba share mounts on boot. You can find the samba share options in the web interface under administration. If you set the startup script option to startup.sh the actual script run after mounting will be /tmp/smbshare/startup.sh

    See also the Samba Filesystem for details.

    Links

    Useful Scripts