'How do I run 1 instance of rsync / rclone script using flock as a cron job?

I'm trying to run only one instance of my back up script as a cron job.
I know I can do it with a function that checks if the process is running:

if pgrep -x rclone >/dev/null
then
    # rclone is still running, backup is not done yet, exit.
    exit
else
    # rclone is not running.
    # start backup.
    /path/to/rclone/script.sh  
fi

But after some research, I found out flock should be used instead of looking for process ID, in crontab -e, in this case, run the script every 30 minutes:
*/30 * * * * /usr/bin/flock -n /var/lock/myjob.lock /path/to/rclone/script.sh

Running the command above requires sudo. Therefore, the script asks for sudo password, and never runs. (I ran the command above manually, that's how I found out).

How exactly do I use flock? Do I type a variable in my script that injects the sudo password when flock asks for it? (I know it's not secure, so there must be a different way to do this).

I tried to research this subject but couldn't find any good answers that explain how to use it properly.

Thank you.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source