I have a small bash script (script.sh) I am trying to run on a schedule using cron. Like so:
#!/bin/bash
lsof -cq -au $(whoami) -t | xargs kill -9
Works perfectly fine when run directly in the shell (bash), i.e. ./script.sh kills all processes under q command for current user.
However, when I try to schedule using cron, not so much anymore.
I came across some answers on the platform suggesting to specify the PATH variable at the top of the cron file, which I have done.
PATH=/usr/sbin/lsof:/bin/xargs:/bin/kill:/bin/whoami:
53 6 * * 1-5 /path/to/script.sh
Regardless, I still get the following output:
...
X-Cron-Env: <PATH=/usr/sbin/lsof:/bin/xargs:/bin/kill:/bin/whoami:>
...
~/script.sh: line 2: xargs: command not found
~/script.sh: line 2: whoami: command not found
~/script.sh: line 2: lsof: command not found
What am I missing?
Thanks
4