Friday, May 3, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 1721  / 1 Year ago, mon, december 26, 2022, 3:06:35

I have an sh file called m.sh with the contents:



echo "Cleaning memory"
sync; echo 3 > /proc/sys/vm/drop_caches
echo ""
free -m

echo ""

ps cax | grep java > /dev/null
if [ $? -eq 0 ]; then
echo "== Server is already running =="
else
sh a.sh
echo "========== STARTING THE SERVER =========="
fi


When I run it with sh m.sh it works perfectly. But I need it to run every 5 minutes, so I am using cron jobs. It needs to work as root/sudo so I did sudo -s, then crontab -e and wrote this in the file:



0,4,9,14,19,24,29,34,39,44,49,54 * * * * /bin/sh /home/<username>/m.sh


I had done some research, and I learned that the cron has different paths than the user, that is why I used those absolute paths, to make sure.



The cron job wasn't running the script every 5 minutes, as it was supposed to.



I also added



* * * * * /bin/echo "Testing123"


to test if cron was working at all, and nothing came up in the console.



How can I make the cron run the script every 5 minutes? I did research online, and tried the solutions, but couldn't make it to work for me. I did service cron start and it said it was already running. I also restarted the service. The permissions are set correctly, I gave all users read, write, and execute permissions just to make sure (I know the permissions are correct).


More From » server

 Answers
1

Regarding your test script -- crontab jobs are not connected to a physical console, so don't expect any output. Try



* * * * * /bin/touch /tmp/foo


... and see whether the /tmp/foo file gets updated. It should.



To get a script running every five minutes, there is a shortcut:



*/5 * * * * /bin/sh /whatever/script/blah.sh


Furthermore, I would edit your script such that it logs its activity. For example,



LOGFILE=/var/log/leoncleaner.log
# ...
echo `date '+%Y-%m-%d %H:%M:%S'` Starting server >> $LOGFILE


Alternatively, you can redirect the output from the crontab file directly:



*/5 * * * * /bin/sh /whatever/script/blah.sh >> /var/log/leoncleaner.log

[#30451] Tuesday, December 27, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
odenanno

Total Points: 207
Total Questions: 113
Total Answers: 94

Location: South Korea
Member since Sat, Oct 2, 2021
3 Years ago
odenanno questions
;