'crontab does not execute a simple shell scripts to turn swap off and turn swap back on

When I execute swapoff -a && swapon -a that works like a charm and when I create a scriptfile swap.sh and I run that it works great too.

chmod 755 swap.sh

But when I make a crontab that should execute the script, than nothing happens.

crontab -e

0 2 * * * /scripts/swap.sh

Am I missing something here?



Solution 1:[1]

Since you confirmed the script is running as super user (sudo) and the file has permissions to execute the problem is other thing (cron in this case):

Try using the full path

/sbin/swapoff -a && /sbin/swapon -a

You have to use full absolute pathnames in crontab commands because when cron runs a script, it does not perform initial login activity (which is where the path(s) are set).

All credits go to: https://ubuntuforums.org/archive/index.php/t-1766875.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 osmarcf