I know, when we run application in shell for a large website, we'd better set ulimit for our shell,
But most of the service is started by systemd/sysv.
Do I need set the ulimit in the service script (/etc/init.d) ?
You would normally set the ulimit on the user the service runs as in something like
For example, if the web service is running as
www-data, you would add an entry for
/etc/security/limits.conf setting the relevant limits.
If the process runs as
root then it's more complicated given the limits in
/etc/security/limits.conf would then apply to all
root owned processes.
One issue with setting the limits in
/etc/security/limits.conf is that it relies on processes going through the PAM stack.
In the case of services and daemons which don't do that, then yes, modifying the relevant service scripts is an acceptable approach. It's probably necessary to do this on a per process basis, and depending on your distribution, service start scripts are usually package managed meaning you'll get conflicts every time you upgrade.