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.
- 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
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@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:
#!/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
.
~
~
: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.
-
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}'
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
.
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
- 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 #