I’m working on many scripts. I add and remove “crontab -e” so many times. I add and remove scripts with
crontab -e so many times. I tried to make an “Available/Enabled” structure for these.
This is my structure’s tree:
<span class="hljs-comment"># all scripts</span> scripts/available/1.sh scripts/available/2.sh scripts/available/3.sh <span class="hljs-comment"># enabled scripts </span> scripts/enabled/1.sh -> scripts/available/1.sh scripts/enabled/2.sh -> scripts/available/2.sh
Then I add this line to “
* * * * * /home/user/scripts/enabled/* &> /dev/null
This is the output of
sudo systemctl status cron.service :
CGroup: /system.slice/cron.service ├─17763 /bin/bash /home/user/scripts/enabled/1.sh /home/user/scripts/enabled/2.sh
At long last:
1.sh is working every minute, but 2.sh is not working.
I want to run scripts only from cron.
What can I do if I don’t want to make a script for “run script in a directory” with another script?
Cron is a time-based job scheduler in Unix-like operating systems. It is used to schedule jobs (commands or scripts) to run periodically at fixed times, dates, or intervals. Running multiple scripts in a folder with cron can be a challenging task. In this blog post, we will discuss how to run multiple scripts in a folder with cron and how to troubleshoot common issues.
The first step in running multiple scripts in a folder with cron is to create a folder structure. In this structure, we will have two folders: “available” and “enabled”. The “available” folder will contain all the scripts that we want to run, and the “enabled” folder will contain symbolic links to the scripts we want to enable.
# all scripts scripts/available/1.sh scripts/available/2.sh scripts/available/3.sh # enabled scripts scripts/enabled/1.sh -> scripts/available/1.sh scripts/enabled/2.sh -> scripts/available/2.sh
Creating Symbolic Links
After creating the folder structure, we need to create symbolic links to the scripts we want to enable. Symbolic links are shortcuts to files or directories. They are created using the ln command with the -s option.
ln -s /home/user/scripts/available/1.sh /home/user/scripts/enabled/1.sh ln -s /home/user/scripts/available/2.sh /home/user/scripts/enabled/2.sh
Running Scripts with Cron
To run multiple scripts in a folder with cron, we need to add a cron job that will run all the scripts in the “enabled” folder. We can do this by adding the following line to the crontab file:
* * * * * /home/user/scripts/enabled/* > /dev/null
This line will run all the scripts in the “enabled” folder every minute. The output of the scripts will be redirected to /dev/null, which means that we will not receive any output.
Troubleshooting Common Issues
When running multiple scripts in a folder with cron, we may encounter some common issues. Here are some tips to help troubleshoot these issues:
Make sure that the scripts and folders have the correct permissions. The scripts should be executable, and the folders should be readable and executable by the user running the cron job.
Make sure that the path to the scripts is correct. When running scripts with cron, the path may be different from the path when running the scripts manually.
Make sure that the scripts are producing output. If the scripts are not producing output, it may be difficult to troubleshoot issues.
Enable logging for the cron job. This will help to troubleshoot issues by providing more information about what is happening during the execution of the cron job.
Check the scripts for errors. If the scripts are producing errors, they may not run correctly with cron.
Running multiple scripts in a folder with cron can be a challenging task. However, by following the steps outlined in this blog post and troubleshooting common issues, we can successfully run multiple scripts with cron. Remember to create a folder structure, create symbolic links to the scripts we want to enable, and add a cron job that will run all the scripts in the “enabled” folder. Happy scripting!
To run multiple scripts in a folder with cron, you can use a wildcard character (
*) in the crontab entry to specify all the scripts in the folder. Here is an example of how you can do this:
- Edit the crontab file by running the command
- Add the following line to the crontab file, replacing
/path/to/scriptswith the actual path to your scripts folder:
* * * * * /bin/bash /path/to/scripts/* &> /dev/null
This will run all the scripts in the
scripts folder every minute.
Alternatively, you can create a wrapper script that runs all the scripts in the
scripts/enabled folder. To do this, create a new script in the
scripts folder, for example
run_scripts.sh, and add the following code:
#!/bin/bash for script in /path/to/scripts/enabled/*; do $script done
Then, in the crontab file, add the following line to run the wrapper script every minute:
* * * * * /bin/bash /path/to/scripts/run_scripts.sh &> /dev/null
This will run all the scripts in the
scripts/enabled folder every minute.
In summary, to run multiple scripts in a folder with cron, you can either use a wildcard character (
*) in the crontab entry to specify all the scripts in the folder, or you can create a wrapper script that runs all the scripts in the
scripts/enabled folder. Both approaches have their own advantages and disadvantages, and you should choose the one that best fits your needs.
It is also worth noting that you can use the
crontab command to manage your cron jobs more effectively. For example, you can use the
-l option to list all the cron jobs, the
-r option to delete all the cron jobs, and the
-u option to specify the user for whom the cron jobs should be run.
I hope this information was helpful. Let me know if you have any further questions or need further assistance.
Alternatively, I can use
run-parts by adding the following line to my crontab file:
* * * * * /usr/bin/run-parts --regex='.+' /home/user/scripts/enabled
In this case, I am specifying the parameter
--regex='.+' which allows any filenames to be included. I can refer to the manual for more information by typing
man run-parts in the terminal.