Support Support Downloads Knowledge Base Juniper Support Portal Community

Knowledge Base

Search our Knowledge Base sites to find answers to your questions.

Ask All Knowledge Base Sites All Knowledge Base Sites JunosE Defect (KA)Knowledge BaseSecurity AdvisoriesTechnical BulletinsTechnotes Sign in to display secure content and recently viewed articles

[MX] Shell script to restart a process with memory leaks by using crontab



Article ID: KB33202 KB Last Updated: 15 Nov 2018Version: 1.0

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.

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.

  1. Log in to the root shell to create the script file by using the vi editor and edit the time of execution in crontab.
jtac@mx-ulab-jtac-1> start shell user root
root@mx-ulab-jtac-1 #
  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 ( in this case).

root@mx-ulab-jtac-1 # cd /root
root@mx-ulab-jtac-1 # pwd
root@mx-ulab-jtac-1 # vi
  1. In the vi editor, type “I” to insert and paste the following:

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
  1. Save and quit by typing ESC, and then :wq.

:wq 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.

  1. For example, the output of the "x" variable is shown as follows. Verify that the daemon that must be killed is correct.

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}'
  1. Add read, execution, and write permissions to the script file that was created.

root@mx-ulab-jtac-1 # chmod 775
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
root@mx-ulab-jtac-1​ #
  1. After the file permissions are added, set the time to execute this script. Use crontab –e.

root@mx-ulab-jtac-1 # crontab -e
  1. A vi editor appears. Type “I” to insert and paste the following script:

0 4 * * * sh /root/
  1. To save and quit, type ESC, and then :wq.

/tmp/crontab.GhqzQldrIy: 1 lines, 102 characters.
crontab: installing new crontab
  1. Check crontab to verify that the script is running properly:

root@mx-ulab-jtac-1 # crontab -l
0 4 * * * /bin/sh /root/
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

  1. To delete crontab, log in to the shell as the root user and run crontab -r. Type yes to confirm.
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​ # 


Comment on this article > Affected Products Browse the Knowledge Base for more articles related to these product categories. Select a category to begin.

Getting Up and Running with Junos

Getting Up and Running with Junos Security Alerts and Vulnerabilities Product Alerts and Software Release Notices Problem Report (PR) Search Tool EOL Notices and Bulletins JTAC User Guide Customer Care User Guide Pathfinder SRX High Availability Configurator SRX VPN Configurator Training Courses and Videos End User Licence Agreement Global Search