There are two obvious issues with your code:
First, you need to use single quotes around the string after echo
to prevent the shell from interpreting the !
in the shebang #!/bin/bash
as the history expansion character which is expanded inside double quotes ... So it needs to look like this:
echo '#!/bin/bash
ulimit -n 600000
ulimit -u 600000
while true; do
sleep 300
bash mybashscript.sh
done' >> mycronscript.sh
You might not notice issues with that when run from inside a script in a non-interactive shell where history is not enabled by default versus when run from an interactive shell ... But, it might/will bite you back(e.g. it will automatically run the last command starting with /bin/bsh
for example if you have previously run some script with /bin/bash scriptfile
that command will be instantly run without asking for your confirmation at all) sometime soon e.g. when your shell settings/environment variables change to enable history in non-interactive shells.
I would, however, use better formatting mechanism alternatives for that with e.g. a Here Document like so:
cat > mycronscript.sh <<EOF
#!/bin/bash
ulimit -n 600000
ulimit -u 600000
while true; do
sleep 300
bash mybashscript.sh
done
EOF
or even the shell builtin printf
(making use of its formatting capabilities) instead of echo
.
Also, your use of the append operator >>
might suggest that mycronscript.sh
already has some lines/code and you're appending to it ... If this is the case then you'll end up with a messy unusable script ... If this is not the case, then use the >
operator to overwrite the contents of the script file.
Second, you would need to run your script with elevated privileges i.e. sudo
in order to increase the max user processes limit or the max open files limit ... etc. using ulimit
above the hard limit ... So it needs to look like this:
sudo bash mainscript.sh
Notice: that if you use sudo
on a script and send it to the background at the same time e.g.:
sudo bash mycronscript.sh &
you might need to bring that back to the foreground with fg
in order to be able to enter the password for sudo
or otherwise the script will be waiting for the password in the background and not executing without you noticing it ... So either run your main forground script with sudo
as described above or you can login to a root shell with e.g. sudo -i
then run your scripts there without sudo
and be very careful as every command you run will run with root
privileges.
I wouldn't be keen on using sudo
inside scripts as well.
Also, please notice that ulimit
is a shell builtin and it provides control over the resources available to processes started under the current shell it's run in and will have no effect on other external shells.
Also, notice that you can lower/increase the soft limit without root
privileges using the -S
option as long as it doesn't exceed the set hard limit ... like so:
ulimit -S -u {N}
where {N}
is a number less than the hard limit ... You can print the hard limit for e.g. user processes with:
ulimit -H -u
or for all limits with:
ulimit -H -a
You can as well lower the hard limit without root
privileges with e.g.:
ulimit -H -u {N}
or, as both -H
and -S
are the default when neither option is specified, with:
ulimit -u {N}
But, you can't raise it again without root
privileges.