BASH SCRIPT NOT RUNNING IN CRONTAB

Bash script

A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ourselves on the command line (such as ls or cp for example)

  1. Create a file with an extension .sh in command line to add script and open with an editor

   # nano /path/to/bash.sh

  1. Start with #!/bin/bash , it’s the first line of script

 The first line tells Unix that the file is to be executed by /bin/bash. This is the standard location of the Bourne shell on just about every Unix system. Adding #!/bin/bash as the first line of your script, tells the OS to invoke the specified shell to execute the commands that follow in the script. #! is often referred to as a “hash-bang”, “she-bang” or “sha-bang”. Though it is only executed if you run your script as an executable. 

  1. Write the script under #!/bin/bash, (which is set of bash commands needed to run the task)

For example : if the script is to create a file with file-name as test.txt and change the ownership

#!/bin/bash 

touch /path/to/test.txt

chown user:user test.txt

Always use full command for proper running of script to get full command we can use “ which “ command

# which touch

Output ; /usr/bin/touch

So the script changes as

#!/bin/bash 

/usr/bin/touch /path/to/test.txt

/usr/bin/chown user:user test.txt

  1. Add execution permission for script

 # chmod +x  /path/to/bash.sh

  1. Run the script 

# ./path/to/bash.sh

          OR

# sh  /path/to/bash.sh

EXPECT SCRIPT

Expect command or expect scripting language is a language that talks with your interactive programs or scripts that require user interaction. Expect scripting language works by expecting input OR output, then the Expect script will send the response without any user interaction.

  1. We need to install expect to run expect script

 # yum install expect

  1. start with #!/usr/bin/expect , it’s the first line of expect script
  2. The commands of expect are work interactively , put commands under first line

Example of expect script and working

# cat /path/to/script.sh

#!/bin/sh
sftp “user”@”ip-address”

#cat path/to/expect.sh

#!/usr/bin/expect

spawn /path/to/script.sh

expect “password:”

send “type-password-here\n”

expect “sftp>”

send “get /file/to/download.file\n”

expect “sftp>”

send “exit\n”

Interact

Output : the above script downloads download.file from ftp 

It starts by running a script.sh contains ftp command to connect 

Commands explained

spawnStarts a script or a program.
expectWaits for program output.
sendSends a reply to your program.
interactAllows you to interact with your program.

Crontab 

Cron is a system daemon used to execute desired tasks (in the background) at designated times

Crontab commands

  1. To add task in crontab

 # crontab -u username -e

=> For expect script

* * * * * /usr/bin/expect /root/koala/expect.sh >/dev/null 2>&1

=> For bash script

* * * * * . $HOME/.profile; /root/koala/host.sh >/dev/null 2>&1

(change the * to required number & add your path to script )

( minute  hour  day-of-month  month  day-of-week  /path/tocommand )

  1. To list running crontab

 # crontab -u username -l

Leave a Reply

Your email address will not be published. Required fields are marked *