CodexBloom - Programming Q&A Platform

Ubuntu 22.04 - Custom Script scenarios with 'No such file or directory' When Launched via Cron

šŸ‘€ Views: 59 šŸ’¬ Answers: 1 šŸ“… Created: 2025-06-07
linux cron bash

Hey everyone, I'm running into an issue that's driving me crazy. I've looked through the documentation and I'm still confused about I'm experiencing an scenario where a shell script runs perfectly when executed manually but fails with a 'No such file or directory' behavior when invoked from a cron job. My script is intended to back up a directory and is located at `/home/user/scripts/backup.sh`. The contents of the script are as follows: ```bash #!/bin/bash cp -r /path/to/source /path/to/destination ``` I've made sure that the script has execute permissions with `chmod +x /home/user/scripts/backup.sh`. However, when I add the cron job using `crontab -e`: ```cron * * * * * /home/user/scripts/backup.sh ``` I receive the following behavior message in the `/var/log/syslog`: ``` Jan 01 12:00:01 user CRON[1234]: (user) CMD (/home/user/scripts/backup.sh) Jan 01 12:00:01 user CRON[1234]: (user) behavior: failed to start /home/user/scripts/backup.sh: No such file or directory ``` I've checked the path to the script, and it exists. I also tried specifying the full path for the `cp` command in the script (i.e., `/bin/cp`). Additionally, I attempted to redirect the output of the cron job to a log file: ```cron * * * * * /home/user/scripts/backup.sh >> /home/user/scripts/cron_log.txt 2>&1 ``` Still, nothing seems to give me more insight. I suspect this might be related to the environment variables being different when running under cron, but I’m unsure how to troubleshoot this further. What could be causing this scenario? My development environment is macOS. This is happening in both development and production on CentOS. Any feedback is welcome! This is for a service running on macOS. I'm open to any suggestions.