making a bash script run through crontab

Quick to answer questions about finding your way around LMDE as a new user.
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions use the other forums within the support section.
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Post Reply
mstrauss2023
Level 1
Level 1
Posts: 43
Joined: Fri Feb 17, 2023 11:07 pm

making a bash script run through crontab

Post by mstrauss2023 »

I have a bash script which mounts a NAS drive, copies a folder from the NAS to a local folder then unmounts the drive. When I double-click the file it opens in a terminal and works perfectly.
I tried running it in crontab but nothing happens. I have many cronjobs which work just fine.

copyshare.sh (yes, it's executable)
#!/bin/bash
sudo mount -t cifs //192.168.1.1/TV\ Shows\ \(4.54TB\) /mnt/nas -o username=*****,password=*****,uid=mike,gid=mike,vers=1.0
cp -fr /mnt/nas/Share_MC /home/mike/backup/nas/
sudo umount /mnt/nas

The location of the script: /home/mike/backup/copyshare.sh
I have tried the following in crontab but nothing happens:
0 18 * * * /home/mike/backup/copyshare.sh
0 19 * * * /usr/bin/gnome-terminal -- /home/mike/backup/copyshare.sh

I also tried putting the script commands into crontab but nothing happens:
0 18 * * * sudo mount -t cifs //192.168.1.1/TV\ Shows\ \(4.54TB\) /mnt/nas -o username=*****,password=*****,uid=mike,gid=mike,vers=1.0
1 18 * * * cp -fr /mnt/nas/Share_MC /home/mike/backup/nas/
15 18 * * * sudo umount /mnt/nas

Any help would be appreciated.

Mike


Runnning:
Linux Mint 21.2 Cinnamon
cinnamon ver 5.8.4
Linux Kernel 5.15.0-78-generic
Processor Intel© Core™ i7-4800MQ CPU @ 2.70GHz × 4
Memory 31.2 GiB
Graphincs Card Intel Corporation 4th Gen Core Processor Integrated Graphics Controller
NVIDIA Corporation GK107GLM [Quadro K1100M]
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: making a bash script run through crontab

Post by Shiva »

mstrauss2023 wrote: Mon Jan 22, 2024 12:18 am
I tried running it in crontab but nothing happens. I have many cronjobs which work just fine.

copyshare.sh (yes, it's executable)
#!/bin/bash
sudo mount -t cifs //192.168.1.1/TV\ Shows\ \(4.54TB\) /mnt/nas -o username=*****,password=*****,uid=mike,gid=mike,vers=1.0
cp -fr /mnt/nas/Share_MC /home/mike/backup/nas/
sudo umount /mnt/nas
Hi,
This post would have been better posted in the Script & bash section.
About your problem now :
At startup, when cronjobs are executed, the system does not know about any user. This comes later, when the environment is set.
So your dest directory name /home/mike/backup/nas/ cannot be solved at startup. The only thing which is known at that moment is a folder named mike in the /System Files/home directory.
Use full path starting from root : "/System Files//home/mike/backup/nas/" with quotes because if I remember correctly, there is a blank between System & Files (cannot test because my locale is not English).
If the script works, the cronjob will certainly do.
User avatar
axisofevil
Level 4
Level 4
Posts: 388
Joined: Mon Nov 14, 2011 12:22 pm

Re: making a bash script run through crontab

Post by axisofevil »

Should sudo umount /mnt/nas be part of your script, it won't work.
mstrauss2023
Level 1
Level 1
Posts: 43
Joined: Fri Feb 17, 2023 11:07 pm

Re: making a bash script run through crontab

Post by mstrauss2023 »

Thanks for the replies guys.

Shiva, you may be right about using cp -fr /mnt/nas/Share_MC "/System Files//home/mike/backup/nas/" in the bash script however the first command in the bash script sudo mount -t cifs //192.168.1.1/TV\ Shows\ \(4.54TB\) /mnt/nas -o username=admin,password=137ab4,uid=mike,gid=mike,vers=1.0 isn't even running.
Therefore I have to assume that the script isn't being called for some reason; I kept an eye on /mnt/nas to see if it ever mounted and it didn't.


as far as
axisofevil wrote: Mon Jan 22, 2024 9:59 am Should sudo umount /mnt/nas be part of your script, it won't work.
In the script this does work because in visudo I entered
mike ALL=(ALL:ALL) NOPASSWD: /bin/umount /mnt/nas

So problem remains: bash script not running as a cronjob.
I've been trying to think of a bash script I can write that can test if it can run as a cronjob. The only thing I could think of was a script using the read command to get user input but that never came up in a terminal even when used with /usr/bin/gnome-terminal --
User avatar
MiZoG
Level 8
Level 8
Posts: 2395
Joined: Fri Jan 26, 2018 8:30 pm
Location: Athens, Greece

Re: making a bash script run through crontab

Post by MiZoG »

In my 20+ years on Linux and with all I know about root filesystem it's the first time I hear about this path: "/System Files//home/...". Capital letters + space in the name is strange to say the least. Lost in translation maybe?
mstrauss2023
Level 1
Level 1
Posts: 43
Joined: Fri Feb 17, 2023 11:07 pm

Re: making a bash script run through crontab

Post by mstrauss2023 »

I just checked the cronjob logs in /var/log/syslog.
I did a search for copyshare and found it how ever there were no errors.
Very puzzling
mstrauss2023
Level 1
Level 1
Posts: 43
Joined: Fri Feb 17, 2023 11:07 pm

Re: making a bash script run through crontab

Post by mstrauss2023 »

Something new which I don't understand:

before I ran crontab -e under my user accoutn mike and the bash script didn't run.

As a test I ran sudo crontab -e and entered the job for the bash script so it would run under sudo to see what would happen.
Much to my surprise it ran.

I don't understand why a bash script runs under sudo and not mike. Mike is a member of sudoers group.

With this new information, does anyone have any thoughts on how I can make it run under the mike account?
User avatar
axisofevil
Level 4
Level 4
Posts: 388
Joined: Mon Nov 14, 2011 12:22 pm

Re: making a bash script run through crontab

Post by axisofevil »

When you ran sudo crontab -e, you entered your password and created a crontab entry for root (because of the sudo).
So all commands worked.

Also - unless things have changed - doesn't cron run using the sh shell, not the bash shell?
Not sure how this would interact with a bash script using sudo.
mstrauss2023
Level 1
Level 1
Posts: 43
Joined: Fri Feb 17, 2023 11:07 pm

Re: making a bash script run through crontab

Post by mstrauss2023 »

About the sh shell, I tried running under mike withh that shell but that didnt work either
Shiva
Level 3
Level 3
Posts: 141
Joined: Thu Jul 07, 2022 11:25 am

Re: making a bash script run through crontab

Post by Shiva »

MiZoG wrote: Mon Jan 22, 2024 4:55 pm In my 20+ years on Linux and with all I know about root filesystem it's the first time I hear about this path: "/System Files//home/...". Capital letters + space in the name is strange to say the least. Lost in translation maybe?
As I said above :
Use full path starting from root : "/System Files//home/mike/backup/nas/" with quotes because if I remember correctly, there is a blank between System & Files (cannot test because my locale is not English).

Seems my remembering was not as accurate as I guessed. And yes, it was just a bold translation of my locale. :wink:
Post Reply

Return to “Beginner Questions”