How to run a script as if it’s running at boot

environment-variablesstartup

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.

Best Answer

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
Related Question