4

I have a script running at boot time, however it's giving me errors I see in the log file. When I run it manually, it runs fine - probably the environment is changed.

Is there a way to run such a script in conditions it's running at boot without restarting?

Script is located in /etc/init.d with a symlink in /etc/rc5.d/S97mounter.sh.

  • 1
    What are the errors, how do you start the script at boot time ( via /etc/init.d/ , /etc/rc.local, crontab entry @reboot)? – Anthon Oct 8 '14 at 8:16
  • Might be looking for su -, which clears the environment unlike su or sudo sh. Of course, that won't solve the problem if it's related to the order in which services have started up. – o11c Oct 8 '14 at 8:17
4

You could run as root

env - scriptname

This will clear your environment before running the script, however, it will also keep your shell. To clear the environment and set the shell to sh, do the following:

env -i /bin/sh -c scriptname

This will then run the script using /bin/sh. However, this will not completely simulate the boot environment as this does not count for the other services that may not be running at the time.

I have found a similar question for simulating the crontab environment and there is a very useful solution posted by mmccoo.

Using this you could run this in a script and reboot the host, then use the environment file to load your environment:

part of a boot script:

env > /var/tmp/bootenv

Then at normal runtime to set the same boot environment, do this:

env - `cat /var/tmp/bootenv` /bin/sh -c scriptname
0

Some things that may be different when a program is started via a user session rather than a startup script:

  • The program inherits many environment variables (including PATH, HOME, …).
  • The program inherits several file descriptors, including a terminal.
  • Limits may be different.

To run a program with a minimal environment and close the standard file descriptors, you can use something like

env -i PATH=/bin:/usr/bin:/sbin:/usr/sbin myprogram </dev/null >/dev/null 2>/dev/null

Check the init man page or other documentation on your system to see what environment variables it defines. Many programs intended to run as daemons will need PATH and nothing else. The PATH value above is the default for init on Debian.

-1

Without more information about what your script is doing and what errors you're getting, it's difficult to give an accurate answer. That being said, it's probably possible to recreate the environment, but it will likely be very difficult, requiring to unmount or bind-mount filesystems or other such fun things.

An alternative could be to create a virtual machine using something like VirtualBox or KVM and perform your debugging using that, allowing you to reboot the virtual machine as often as require without requiring you to reboot your host machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.