Add below code in /etc/rc.local
of the linux VM's image:
####Marker_start####
sed -i '/####Marker_start####/,/####Marker_end####/d' /etc/rc.local
#Your custom code here
####Marker_end####
Explanation:
- rc.local script gets executed after every system start. Hence, adding the code there will cause it to execute after boot.
- Use sed to remove the custom code first time the script executes. Thus, the code is executed only once.
Other approach:
Create a script containing your custom code & append below code to it.
chmod 644 "$0"
& create a symlink
ln -s /path/to/your-script.sh /etc/rc5.d/S99my-custom-script.sh
(Assuming your default runlevel is 5. Change it as required on your VM's distro - e.g. ubuntu has it on /etc/rc2.d
)
Explanation:
Scripts named /etc/rc5.d/S*
are executed automatically when entering run-level 5, with argument=start
.
Upon first execution, make the script as non-executable, so that it gets executed only once.