A process can at times have memory leaks and a fix for the problem may not be available until the next release upgrade. In such cases, customers can install a shell script to restart the problematic process at a specified time by using crontab .
crontab
This article details how to create the shell script that can be used to restart the process, and how to install the script as a workaround until a fix is made available.
A memory leak is observed in certain processes within Junos OS.
Note: Root access is required to run this procedure.
root
jtac@mx-ulab-jtac-1> start shell user root Password: root@mx-ulab-jtac-1 #
When in the root shell, move to the /root directory and open a vi editor with the name of the script to be created ( script.sh in this case).
/root
script.sh
root@mx-ulab-jtac-1 # cd /root root@mx-ulab-jtac-1 # pwd /root root@mx-ulab-jtac-1 # vi script.sh
In the vi editor, type “ I ” to insert and paste the following:
I
#!/bin/sh x=`ps auxww | grep mosq | grep -v grep | awk 'NR==1{print $2}'` y=`ps auxww | grep mosq | grep -v grep | awk 'NR==2{print $2}'` z=`ps auxww | grep mosq | grep -v grep | awk 'NR==3{print $2}'` kill $x echo "mosquitto $x restarted" `date` >> /var/tmp/mosquitto-restart-track kill $y echo "mosquitto $y restarted" `date` >> /var/tmp/mosquitto-restart-track kill $z echo "mosquitto $z restarted" `date` >> /var/tmp/mosquitto-restart-track
Save and quit by typing ESC , and then :wq .
ESC
:wq
~ ~ :wq script.sh: 11 lines, 338 characters. root@mx-ulab-jtac-1 #
In the above example, three mosquitto-nossl processes were found that had to be restarted. These variables are named x, y, and z for differentiation. Note that variables can be added, removed, and killed. Or an echo can be used for the variable in order to match one or more processes. echo is used to generate a file under /var/tmp with the time that the process was restarted.
mosquitto-nossl
echo
/var/tmp
For example, the output of the " x " variable is shown as follows. Verify that the daemon that must be killed is correct.
x
root@mx-ulab-jtac-1 # ps auxww | grep mosq | grep -v grep daemon 39087 0.0 0.0 22712 1752 - S Wed06AM 0:34.75 /usr/libexec/mosquitto-nossl -c /var/run/mqtt_broker_re.conf root@mx-ulab-jtac-1 # ps auxww | grep mosq | grep -v grep | awk 'NR==1{print $2}' 39087
Add read, execution, and write permissions to the script file that was created.
root@mx-ulab-jtac-1 # chmod 775 script.sh root@mx-ulab-jtac-1 # ls -la total 28 drwxr-xr-x 2 root wheel 512 May 09 19:58 . drwxr-xr-x 24 root wheel 512 May 09 12:52 .. -rwxrwxr-x 1 root wheel 338 May 09 20:28 script.sh root@mx-ulab-jtac-1 #
After the file permissions are added, set the time to execute this script. Use crontab –e .
crontab –e
root@mx-ulab-jtac-1 # crontab -e
A vi editor appears. Type “ I ” to insert and paste the following script:
0 4 * * * sh /root/script.sh
To save and quit, type ESC , and then :wq .
:wq /tmp/crontab.GhqzQldrIy: 1 lines, 102 characters. crontab: installing new crontab
Check crontab to verify that the script is running properly:
root@mx-ulab-jtac-1 # crontab -l 0 4 * * * /bin/sh /root/script.sh root@mx-ulab-jtac-1 #
In this example, crontab is scheduled to run at 04:00 hours every day. After crontab is installed, the script will run at the time specified, and a log file will be generated as follows:
jatc@mx-ulab-jtac-1> file show /var/tmp/mosquitto-restart-track mosquitto 7673 restarted Sat Sep 22 13:42:00 UTC 2018 mosquitto 7675 restarted Sat Sep 22 13:42:00 UTC 2018 mosquitto 7684 restarted Sat Sep 22 13:42:00 UTC 2018
Deleting crontab
crontab -r
yes
root@mx-ulab-jtac-1 # crontab -r remove crontab for root? yes root@mx-ulab-jtac-1 # crontab -l crontab: no crontab for root root@mx-ulab-jtac-1 #