How to pass arguments to shell script in crontab ?
Last Updated :
23 Nov, 2022
In this article, we will discuss how to schedule shell scripts in crontab and to pass necessary parameters as well.
First, let's create a simple script that we will be scheduled to run every 2 minutes. The below is a simple script that calculates the sum of all parameters passed and prints them to STDOUT along with the time the script was run.
#! /usr/bin/sh
SUM=0
for i in $@
do
SUM=`expr $SUM + $i`
done
echo "$SUM was calculated at `date`"
Note: #! /usr/bin/sh (specifying the path of script interpreter) is necessary if wish to make the script executable.
Assuming we have saved this script as my_script.sh under our home directory, we can make it executable by entering the following command in our terminal:
chmod +x my_script.sh
We can test our script if it is working properly:
./my_script.sh 1 2 3 4 5
15 was calculated at Thursday 01 July 2021 01:24:04 IST 2021

The crontab scheduling expression has the following parts:

To schedule our script to be executed, we need to enter the crontab scheduling expression into the crontab file. To do that, simply enter the following in the terminal:
crontab -e
You might be prompted to select an editor, choose nano and append the following line to the end of the opened crontab file:
*/2 * * * * /home/$(USER)/my_script.sh 1 2 3 4 5 >> /home/$(USER)/output.txt

where $(USER) can be replaced with your username. Save changes and exit. This will schedule our shell script to run every 2 minutes with 1 2 3 4 5 as command-line arguments and write the output to /home/$(USER)/ouput.txt.
Note: There are a few things to keep in mind before scheduling cron jobs:
- All cron jobs are scheduled in the local time zone in which the system where the jobs are being scheduled operates. This could be troublesome if jobs are being scheduled on servers with multinational personnel using it. Especially if the users also belong to countries that follow Daylight Savings Time practice.
- All cron jobs run in their own isolated, anonymous shell sessions and their output to STDOUT (if any) must be directed to a file if we wish to see them.
- All cron jobs run in the context of the user for which they were scheduled. It is therefore always good practice to provide an absolute path to scripts and output files to avoid any confusion and cluttering.
Similar Reads
How to print command line arguments passed to the script in Node.js ? Node.js is an open-source and cross-platform runtime environment built on Chrome's V8 engine that enables us to use JavaScript outside the browser. Node.js helps us to use build server-side applications using JavaScript. In Node.js if you want to print the command line arguments then we can access
2 min read
How to Create a Shell Script in linux Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
Bash Script - How to use Command Line Arguments In this article, let us see about Command Line Arguments usage in Bash script. Arguments are inputs that are necessary to process the flow. Instead of getting input from a shell program or assigning it to the program, the arguments are passed in the execution part. Positional Parameters Command-line
4 min read
How to Print Second Command line Argument in shell? Command line arguments are the parameters we pass to a command or script when we run it from the terminal. These arguments are passed as strings and can be accessed within your program to perform various operations. How to Print Second Command Line Argument in Shell?The shell stores the command line
5 min read
Sending Command Line Arguments to NPM Script In the sector of NodeJS development, NPM (Node Package Manager) scripts are a effective tool for automating numerous tasks like strolling exams, building the mission, or starting a improvement server. However, there are times while you need to bypass arguments to these scripts to customize their beh
3 min read
How to Use Heredoc in Shell Scripting The Heredoc or Here Document is an input literal used by many programming and scripting languages. The Heredoc was originally used in UNIX Shells and is in fashion till date and, is supported by all popular Linux shells such as zsh, bash, tsh, etc. The symbol for a Heredoc is '<<' two left che
6 min read