Bashrc, Shell Script and Crontab

Bashrc, Shell Script and Crontab

I'm trying to use the functions defined in /home/my_username/.bashrc in a shell script that gets executed by crontab.

#crontab -l

# m h  dom mon dow   command
  * *  * * * /bin/sh /home/my_username/CronTab_shell_script.sh >> /home/my_username/Desktop/file.log  

Let's say I have the mounted() function saved in

/home/my_username/.bashrc 

function mounted(){

    if mount|grep $1; then
    echo "mounted"
    else
    echo "not mounted"
    fi

 }

How can I call and use the mounted() function from the

/home/my_username/CronTab_shell_script.sh
1

2 Answers

1. Run the correct shell

* *  * * * /bin/sh .....

You are not running bash, you are running sh, so you should not expect .bashrc to be loaded!

Try this instead:

* *  * * * /bin/bash .....
Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.