[SOLVED] Drop cache job with certain condition doesnt work in crontab

altayaltan

New Member
Aug 4, 2020
15
2
3
32
Hi,

I'm trying to create a cronjob to run every minute to drop cache checking a certain condition to be met and i don't want them to overlap if it takes too long so i tried using flock. The cronjob checks if the used ram percentage is less than given value and used ram+cache ram percentage is more than given value then executes. It's like this:

Code:
* * * * * flock -n /tmp/cache.lock -c "if (( $(free -m | grep Mem | awk '{print $3/$2 * 10000000}') < 465000 )) && (( $(free -m | grep Mem | awk '{print ($3+$6)/$2 * 10000000}') > 465000 )); then sync; echo 3 > /proc/sys/vm/drop_caches; fi"

So after enabling this, every minute i got an email like this one below:

/bin/sh: 1: cannot open 465000: No such file
/bin/sh: 1: 455583: not found

When i run the command manually from cli it works fine, when it's a cronjob this happens. Can someone help please? The very same code works fine with other linux os types like centos or ubuntu is it something about debian?

Edit:

I also tried the same with a single ping command to run for 300 seconds every minute with flock and it seems to work fine.Cronjob:

Code:
* * * * * flock -n /tmp/ping.lock -c "timeout 300 ping 8.8.8.8"

Output:

Code:
fuser -v /tmp/ping.lock
                     USER        PID ACCESS COMMAND
/tmp/ping.lock:      root      54422 f.... flock
                     root      54423 f.... sh
                     root      54425 f.... timeout
                     root      54426 f.... ping

But doesnt work with the drop cache command.
 
Last edited:
without explicitly trying your cronjob - my guess is that the script uses a few bashisms (shell-script syntax which only works with bash), while the default shell for running cronjobs is 'dash' (which is smaller, but does not support all bash-specifics)
as a quick test try adding
Code:
SHELL=/bin/bash
above your cronjob

if this works - either keep it set, or rewrite the script to be POSIX shell compatible (I like `shellcheck` as a linter)

I hope this helps!
 
without explicitly trying your cronjob - my guess is that the script uses a few bashisms (shell-script syntax which only works with bash), while the default shell for running cronjobs is 'dash' (which is smaller, but does not support all bash-specifics)
as a quick test try adding
Code:
SHELL=/bin/bash
above your cronjob

if this works - either keep it set, or rewrite the script to be POSIX shell compatible (I like `shellcheck` as a linter)

I hope this helps!
Thanks for your reply, but i seem to solve my problem a couple of minutes ago. I guess the "print" statement before the functions mess up the command. I removed that and added parantheses around the functions like this:

Code:
* * * * * flock -n /tmp/cachedrop.lock -c "if (( $(free -m | grep Mem | awk '{($3/$2) * 10000000}') > 340000 )) && (( $(free -m | grep Mem | awk '{(($3+$6)/$2) * 10000000}') > 340000 )); then sync; echo 3 > /proc/sys/vm/drop_caches; fi"

Now it works without issues. No error logs.
 
Great that you found the solution! Thanks for sharing it.

Please mark posts as 'SOLVED' if you find a solution (for the next time - this time I already did it)

Thanks!
 
  • Like
Reactions: altayaltan
For the record in centos the command on both cli and crontab works with the first version. In ubuntu and debian the command works as is on cli. But when you want to apply this to crontab you need to edit the command as in the solution.
 
  • Like
Reactions: Stoiko Ivanov

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!