In my previous post I demonstrated how easy it was to start a script on boot using systemd
in Linux.
In this post I am going to show how you can set ulimits
for the same script at startup. I will use the same script I used earlier even though it does not need any higher limits.
I have a script monitor.sh
that simply runs a curl command in an endless while loop. This particular example does not do anything other than run the curl, but you could easily add a check and send alert if the curl statement fails
Let’s write our monitor.sh script first which runs the curl in an endless loop. Using an editor you are comfortable with, save the following code in a file /home/ubuntu/monitor.sh
|
|
Make sure you have execute permission set.
|
|
Next, we will set up the systemd configuration file. Create a new file sudo vi /etc/systemd/system/mymonitor.service
.
|
|
Let’s examine the new lines I added to the configuration.
In the [Service]
section I added LimitNOFILE to set soft limit of 1024 and hard limit of 4096. This sets the soft and hard limit for number of open files by this process.
If you just want to set a higher hard limit you could change it to LimitNOFILE=10240
Let’s go ahead and reload the systemd daemon to ensure it picks up the new configuration file. Stop and start the service.
|
|
Now Let’s check the ps and limits again.
|
|
You can see that the soft and hard limit has changed for this process.
We can also set other limits if we wanted to change them. See this page for other properties
Conclusion
Changing ulimits
for a process that starts up on boot can be easily done in your systemd
unit file.
Note:
- If you make any changes to your script monitor.sh, make sure to stop and start your service so that a new process is launched.
- Disable the service if not needed.