How to Set Up a Cron Job with Special Characters
A Cron job is a task that is scheduled to run on a regular interval to automate various functions within your website.
A cron job automatically executes a list of defined functions such as file backups, software updates, or email dispatches.
So, without a cron job, you have to manually run backups, and software updates and send emails one by one to the recipient on a particular event which is time-consuming and tedious work.
So, setting up a cron job is essential as it is a very important functionality for every website.
Disclosure: I may receive affiliate compensation for some of the links below at no cost to you if you decide to purchase a paid plan. You can read our affiliate disclosure in our privacy policy.
Video Tutorial
How to Create a CRON Job?
To create a cron job, you need to edit a file called the crontab, which contains the commands and the schedule for each job.
The format of a cron job is:
minute hour day month weekday <command-to-execute>
You can use wildcards, ranges, steps, or special strings to specify the time or frequency of the job.
For example, if you want to run a backup script every day at midnight, you can use:
0 0 * * * /path/to/backup.sh
If you want to run a job every 15 minutes on weekdays, you can use:
*/15 * * * 1-5 /path/to/job.sh
Some examples of special strings are:
@reboot to run a job once at startup
@daily to run a job once a day
@weekly to run a job once a week
@monthly to run a job once a month
@yearly to run a job once a year
The Easy way is to go to this amazing Cron Generator Tool.
Here is a Real Demo of a Cron Code
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
How to Setup a Special Characters Cron Code in Hostinger
If you are using some other hosting where the special characters are not accepted in the cron code then you can follow the procedure mentioned below to solve it.
The Process is separated into 2 steps
- Step 1: Creating a Bash File for Cron Job
- Step 2: Setting up a Cron Job
Step 1: Creating a Bash File for Cron Job
We will create a bash file so that the cron can access the file an execute it automatically. The bash file will contain our cron code with special characters. Which was not accepted earlier by the cron job creator in your hosting.
To create a Bash file for cron job, follow these steps
- Login to your cPanel/Hosting Panel.
- Access the File Manager and Navigate to the Root Folder (Open public_html ).
- Now create a New File > Cron.sh (making sure to use the .sh extension)
- Now open the cron.sh file in Editor Mode (You need to paste codes here)
- Add the command to your .sh file. Here’s an example:
#!/bin/sh
/usr/bin/php /home/u123456789/domains/example.tld/public_html/scripts/scheduled.php > /dev/null 2>&1
Where:
- !/bin/sh – indicates that the cron job file is being opened.
- /usr/bin/php – loads the PHP libraries (these are required if you are executing a PHP script)
- /home/u123456789/domains/example.tld/public_html/scripts/scheduled.php > /dev/null 2>&1 – the actual command that you want to be executed.
Replace the path and filename with the actual file location and name on your hosting
When all is good to go, apply the changes to your file before exiting by clicking on the save icon.
Note: Our tool will help you create this complicated code in an easy step-by-step process. Visit Cron Generator Tool
Step 2 – Set up a Cron Job
Now that the Bash file with the command is ready, set up the Cron Job. Open Hostinger Panel > Select the Websites → Manage, search for Cron Jobs on the sidebar and click on it:
Choose Custom, and insert your command like this:
/bin/sh /home/u123456789/domains/example.tld/public_html/cron.sh
Note: The Cron Generator Tool will generate this URL for you.
Where:
- /bin/sh – indicates to execute a .sh file.
- /home/u123456789/domains/example.tld/public_html/scripts/bash.sh – is the path to the actual .sh file to be executed. As this is an example, ensure to replace it with the actual path to the file corresponding to your hosting.
Next, select the schedule when the Cron Job will be executed from the Common options or add it manually:
Which will be a minute hour day month weekday.
That’s it. That’s how you will create a Cron Job with Special Characters in Hostinger.