Running a shell script during boot on Junos
I had recently to work on a situation where specific Junos configuration had to be applied on a device after reboot as a workaround.
There are multiple solutions to this problem and the simplest one is to create a shell script that crontab will run after reboot.
However, you need to make sure that you wait long enough for the device to load Junos before you can apply the configuration.
crontab starts 7-8 seconds after the device starts the booting process and you need a sleep command to delay the script applying the configuration.
This is the testing script. The script starts with a sleep of 240 seconds, then enters the configuration mode, add an interface followed by commiting the configuration:
#!bin/sh logger "STARTING sleep 240s to complete boot process" sleep 240 logger "AFTER 240s" logger "STARTING configuration using script" /usr/sbin/cli -c 'configure;set interfaces lo0 unit 0;commit comment "Commit by script"'
This is the crontab configuration. Here, the keyword of interest is “@reboot”. crontab will run the commands only after a reboot:
root@QFX5100:RE:0% crontab -l @reboot /bin/sh /root/boot_script.sh root@QFX5100:RE:0%
After reboot, in about 7-8 seconds, crontab runs the script and actually enter a sleep period of 240 seconds to make sure that the cli commands will be applied only after the device completed the boot sequence:
root@QFX5100:RE:0% cat /var/log/messages | grep "SYSTEM_OPERATIONAL\|STARTING\|AFTER\|script" Jun 8 13:24:43 QFX5100 eventd[1116]: SYSTEM_OPERATIONAL: System is operational Jun 8 13:24:50 QFX5100 root: STARTING sleep 240s to complete boot process root@QFX5100:RE:0%
While waiting to pass the sleep interval, you can check that the script is still running:
root@QFX5100:RE:0% ps aux | grep boot root 1491 0.0 0.1 2256 1472 ?? Is 1:24PM 0:00.00 /bin/sh -c /bin/sh /root/boot_script.sh root 1501 0.0 0.1 2252 1468 ?? I 1:24PM 0:00.00 /bin/sh /root/boot_script.sh root@QFX5100:RE:0%
After 240 seconds, the script is applying the configuration:
root@QFX5100:RE:0% cat /var/log/messages | grep "SYSTEM_OPERATIONAL\|STARTING\|AFTER\|script" Jun 8 13:24:43 QFX5100 eventd[1116]: SYSTEM_OPERATIONAL: System is operational Jun 8 13:24:50 QFX5100 root: STARTING sleep 240s to complete boot process Jun 8 13:28:52 QFX5100 root: AFTER 240s Jun 8 13:28:52 QFX5100 root: STARTING configuration using script Jun 8 13:28:52 QFX5100 mgd[1852]: UI_COMMIT: User 'root' requested 'commit' operation (comment: Commit by script) root@QFX5100:RE:0%
The configuration was applied successfully by the script:
{master:0}[edit] root@QFX5100# show | compare rollback 1 [edit interfaces] + lo0 { + unit 0; + } {master:0}[edit] root@QFX5100#
Once the script finishes, it not longer runs.
root@QFX5100:RE:0% ps aux | grep boot root@QFX5100:RE:0%
And this would be all about how you can invoke a shell script during the boot process of a Junos device.
I hope you found this interesting and in a future post I will show how you can invoke a shell script from a SLAX script.
Paris ARAU
Latest posts by Paris ARAU (see all)
- Junos Fusion – Part IV – Satellite policies and uplink failure detection - 30 July 2018
- Junos Fusion – Part III – Satellite commands and traffic forwarding - 16 July 2018
- Junos Fusion – Part II – Configuration, Administration and Operation - 16 July 2018
- Junos Fusion – Part I – Overview, Components, Ports and Software - 11 July 2018
- Vagrant – Part IV – Network topology using Juniper and Cumulus - 26 April 2018
Thanks for the post Paris. I need to run a script after my SRX restarts, We are running 12.3X48-D36 on a SRX 210HE2.
Everything in the /root folder gets deleted on reboot, so I cannot put the script there. I setup a crontab as
root@MKC-Main% crontab -l
@reboot /bin/sh /jail/var/etc/httpd_config_replace.sh
root@MKC-Main%
My script is executable, but does not run
root@MKC-Main% ls -l /jail/var/etc/httpd_c*
-rwxr-xr-x 1 root wheel 343 Jul 20 23:42 /jail/var/etc/httpd_conf_replace.sh
-rw-r–r– 1 root wheel 43 Jul 21 00:17 /jail/var/etc/httpd_conf_replace.txt
root@MKC-Main%
Thoughts?
Walt
Hi Walt,
Can you run the script manually using the same crontab command, “/bin/sh /jail/var/etc/httpd_config_replace.sh” ?
I guess you noticed that you need to wait some time after the devices starts the bootup process before you can actually configure the device through a script.
Do you see any messages written using logger when the script should be launched?
Thanks,
Paris