Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Adding a script to startup on a VPS

zjenzjen Member
edited February 2012 in Help

What's a good way to add a script to startup so that it runs as if you had typed the commands yourself over SSH?

I'd like to create some screen sessions and run commands in them that I can then SSH in and attach to later.

One problem I'm having is that I'll add a script to crontab or /etc/init.d and the command will fail, I think because the environment is different e.g. different environmental variables. Also adding to init.d isn't quick and easy.

Comments

  • Hmmm, debian stuff? Well, then

    If you want something running at boot, yes, you can put it at /etc/rc.local That is at boot. The alternative is to create a custom script, put it in init.d as you say AND symlink it to the corresponding folders rcX.d where 'X' is a number showing the run level of the system. If you want this to be easy, put it in init.d and then use something like

    update-rc.d -f myscript defaults
    

    That will symlink your script for you to each runlevel =P

    But if is Ubuntu stuff, sometimes this doesn't work, because recent versions uses that weird "upstart" instead of init, and supossedly are compatible... but personally I have issues or I don't understand how it works.

    But I have some questions now because you firsat say SSH, and after you say GUI.

  • Yes SSH, no GUI.

    Don't update-rc.d 'defaults' run at shutdown too? Run levels are very unclear to me, despite reading the man pages.

    Do I need to set the PATH variable in my script or use the full paths to any executables my script runs? Can I use su -l or another command to set up the environment as if I was logged in?

    Thanks

  • Add it to /etc/rc.local, be sure to add an & after the script to background it :P

  • Can I see it in jobs or return it to the foreground somehow if I do that?

  • Is better to use full paths. The default path is limited to /bin /sbin and maybe that's all.

    Honestly, I am not sure if it runs at shutdown too, I haven't tried. But you always can remove the right symlinks. When you apply the command it will show what symlinks has created, then you can remove them (maybe 0 and 6 for shutdown and reboot?)

  • zjenzjen Member
    edited February 2012

    I think you are in the same position as me! I know the supposed startup methods but not sure if I'm doing it correctly. Run levels are confusing.

    Based on maxexcloo's advice I'll try rc.local instead of cron and init.d.

  • crontab -e and put it after a @reboot

  • zjenzjen Member
    edited February 2012

    Why though? Since it's a LEB, perhaps I want to disable cron.

    Does anyone else run screen on startup or anything similar?

  • SpiritSpirit Barred
    edited February 2012

    Since it's a LEB, perhaps I want to disable cron.

    How much do you gain with disabling cron? :)

    @OneTwo said: crontab -e and put it after a @reboot

    That's also my preference disregarding distro.

    crontab -e
    @reboot cd /root; ./yourscript.sh >/dev/null 2>&1
    ...and voila!

  • OneTwoOneTwo Member
    edited February 2012

    @zjen cron doesn't take more than 1-2MB ram.

    @Spirit you could use:
    @reboot /root/yourscript.sh >/dev/null 2>&1

  • Thanks. Anything special in yourscript.sh like setting PATH?

  • @OneTwo but then the script's working directory wouldn't be /root would it?

    Do you really need to pipe the output to /dev/null?

  • @zjen I don't know about the directive, you don't have to set anything special in the script. You could always have a "cd /path/path" to go where you want.

    If there's output without nulling it, you'll get an email with it.

  • SpiritSpirit Barred
    edited February 2012

    @OneTwo said: you don't have to set anything special in the script.

    @OneTwo, #!/bin/sh line to state the interpretor explicitly maybe? Not mandatory but well...

  • @Spirit that's obvious. at least for me.

Sign In or Register to comment.