0

I have this script in /etc/init.d that I want to know if it will be run when the system reboots. Is there a way to "simulate" the conditions of a reboot to see if the script will actually be invoked (and to debug it) without having to actually reboot the system?

Please note that I'm aware that you can just run the script to debug it. My point is to troubleshoot those cases when the script runs ok when run manually but, for some reason, it is not run when the system starts/reboots (for example, some run condition with another script, things with permissions, etc).

Maybe this has to do with runlevels? As I am not that familiar with them.

Thanks!

jotadepicas
  • 133
  • 1
  • 6
  • 2
    The problem with such a simulation would be that it is not feasible to enumerate all the things which could go wrong. Short of replicating your entire system in a virtual machine, I don't see how it could be done. – kasperd Mar 27 '16 at 17:18
  • Didn't Ubuntu also migrate away from SysVinit scripts to systemd? – HBruijn Mar 27 '16 at 17:43
  • @HBruijn yes but AFAIK, they both still work, and backwards compatilibity is guaranteed is you use the `service` command which works with both. – jotadepicas Mar 27 '16 at 20:59

2 Answers2

1

A common issue is environment differences. If the script does not have a bang path on the first line, then shell differences may be an issue.

The most common issue I have experienced is path expectations. init.d scripts run with a stripped down path.

These are the same issues that cron scripts have. I believe the path and environment are the same or at least quite similar. Try running a cron job as root that dumps its environment. Something like:

echo PATH=$PATH
echo
echo ENVIRONMENT
env
echo
echo Set variables
set

Then start a /bin/sh session and clear its environment to match that of the cron job before running your init job. You should be able to script the setup and run as a /bin/sh script.

BillThor
  • 27,354
  • 3
  • 35
  • 69
1

Although it may be ugly way to debug but you can actually try to enable your service on unused runlevel to check how script behaves when invoked by init.

E.g. usually, runlevel 4 is not used (although, check on your system). Then you can copy symlinks directory rc3.d to rc4.d (to make sure levels 3 and 4 have all services same) and enable your service for level 4. Then, by running telinit 4 you can emulate start of your script. This will cause init to switch runlevel which is almost similar to when it switches to reboot runlevel prior to machine restart.

Note, that answer from @BillThor correctly suggests key issues and may state more convenient way to debug your script than pulling init each time as it anyways will do the work of restarting your services each time.