LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2020, 04:32 PM   #1
goldennuggets
Member
 
Registered: Feb 2003
Location: USA
Distribution: Kubuntu, Manjaro
Posts: 239

Rep: Reputation: 24
Unhappy Bash Script - Breaks with Cron


I'm running Kubuntu.

I've written a bash script that backs up my configuration files, 7zips them, uploads the archive to dropbox, and then deletes the local archive.

It runs beautifully.

I then added a cron job to have this script executed weekly. It always places the 7zip file in my home directory and then the file doesn't get uploaded. I've tried using absolute directory references rather than relative ones all to no avail.

It runs fine when I manually execute it. But it breaks when cron runs it.

It's driving me mad! Has anyone else experienced this and do you have any help/suggestions/tips/answers as to how to make the script work when it's run as a cron job?
 
Old 02-11-2020, 04:39 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Usually things like this occur due to the differing environment when run as cron, typically paths. You say that you have used absolute paths, but no one can tell you much more without actually seeing the script.

Please post a minimal example script which illustrates the actual problem.
 
Old 02-11-2020, 04:42 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Such behavior is almost always about the lack of an environment when running from cron.
You mention using absolute paths...does that include absolute paths to the executables as well as the source and zipped files?
 
Old 02-11-2020, 04:48 PM   #4
goldennuggets
Member
 
Registered: Feb 2003
Location: USA
Distribution: Kubuntu, Manjaro
Posts: 239

Original Poster
Rep: Reputation: 24
Here's an example that has been simplified with less backup directories and my username removed. The zip file creates perfectly fine, with all of the files I want it in, but then nothing else happens.

Now that I'm looking at this, is it possible that cron is not executing the command "droptobox" due to privilege issues? If so, what's the easiest way to correct that while still be secure?

Code:
#!/bin/zsh
# Purpose: To backup my configuration files on a weekly basis and upload to dropbox.

_name=$(date '+%Y-%m-%d')
# Compress all configuration files into on archive
7z a /home/username/$_name.7z /home/username/.zshrc /home/username/.vimrc /home/username/.tmux.conf /etc/samba/smb.conf /etc/fstab 

# Upload archive to Dropbox and delete compressed archive
droptobox upload /home/username/$_name.7z /ConfigBackups/ && rm /home/username/$_name.7z
 
1 members found this post helpful.
Old 02-11-2020, 09:16 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,877
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
https://www.linuxquestions.org/quest...0/#post5707383
 
1 members found this post helpful.
Old 02-11-2020, 10:10 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
I am not a dropbox user so cannot comment on how it handles permissions, but if your example reproduces the problem then that is a very good place to start!

The link and posts provided by NevemTeve above are a very good reference for troubleshooting your shell scripts, give those a look!
 
Old 02-11-2020, 10:48 PM   #7
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,738

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
1. which dropbox to determine the absolute path to that command.
2. put the absolute path in your script.
 
Old 02-12-2020, 04:13 PM   #8
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 419

Rep: Reputation: Disabled
As others have mentioned, use absolute paths in you script. It will NOT run as a cron job as you have shown here. Running it as a user of some sort it has a PATH variable in the environment. Running it as a cron job, regardless of the user, it has NO PATH to refer to so the programs simply cannot be found. Find out where they are on your machine (/usr/bin/, /usr/local/bin/ or wherever) and use the complete path for each program.
 
Old 02-12-2020, 06:26 PM   #9
goldennuggets
Member
 
Registered: Feb 2003
Location: USA
Distribution: Kubuntu, Manjaro
Posts: 239

Original Poster
Rep: Reputation: 24
Alright! I'm going to give it a go. That makes perfect sense.
I've added the absolute path in and (I'm a bit lazy) so I'm waiting until Monday for the next run...
That's why I made the script in the first place...
 
Old 02-13-2020, 12:11 AM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,877
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Mind you, if it is a GUI-application then it certainly won't work from cron.
 
Old 02-13-2020, 06:06 AM   #11
goldennuggets
Member
 
Registered: Feb 2003
Location: USA
Distribution: Kubuntu, Manjaro
Posts: 239

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by NevemTeve View Post
Mind you, if it is a GUI-application then it certainly won't work from cron.
Absolutely. Thanks for the reminder! This is 100% terminal and can run behind the scenes.
 
Old 02-15-2020, 08:52 AM   #12
Steve R.
Member
 
Registered: Jun 2009
Location: Morehead City, NC
Distribution: Mint 20.3
Posts: 521

Rep: Reputation: 98
Quote:
Originally Posted by goldennuggets View Post
I'm running Kubuntu.

I've written a bash script that backs up my configuration files, 7zips them, uploads the archive to dropbox, and then deletes the local archive.

It runs beautifully.

I then added a cron job to have this script executed weekly. It always places the 7zip file in my home directory and then the file doesn't get uploaded. I've tried using absolute directory references rather than relative ones all to no avail.

It runs fine when I manually execute it. But it breaks when cron runs it. (emphasis added)

It's driving me mad! Has anyone else experienced this and do you have any help/suggestions/tips/answers as to how to make the script work when it's run as a cron job?
I experienced a similar problem. The problem was caused by using an environmental variable. In my case, the use of, "$SECONDS". From your post it is unknown if any environmental variables were or were not used. See this old post. Bash script works in terminal but not in cron.
 
Old 02-18-2020, 11:40 AM   #13
goldennuggets
Member
 
Registered: Feb 2003
Location: USA
Distribution: Kubuntu, Manjaro
Posts: 239

Original Poster
Rep: Reputation: 24
I've tried with the absolute paths and it still doesn't work.
This time it didn't even zip the file up like it has in the past.
 
Old 02-18-2020, 12:27 PM   #14
Steve R.
Member
 
Registered: Jun 2009
Location: Morehead City, NC
Distribution: Mint 20.3
Posts: 521

Rep: Reputation: 98
Have you checked your permissions? When a program runs from cron, it usually executes as root. When you run a program from terminal, you are usually running it as yourself. Unless you are executing the script with sudo.
 
Old 02-18-2020, 12:30 PM   #15
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by goldennuggets View Post
I've tried with the absolute paths and it still doesn't work.
This time it didn't even zip the file up like it has in the past.
What doesn't work? You need to show us the script and the relevant directory and file permissions or we can't help.
 
  


Reply

Tags
automation, bash script, cron, scripting



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Force breaks to avoid eye strain (typing breaks) upnort Slackware 12 09-19-2019 03:43 PM
[SOLVED] Ways to prevent cron e-mails from particular bash script located in cron.daily? postcd Linux - General 2 08-22-2017 08:55 AM
adding a perl script to cron.daily / cron.d to setup a cron job CrontabNewBIE Linux - Software 6 01-14-2008 08:16 AM
cron not working from crontab nor form /etc/cron/cron.d. What did SuSE change? JZL240I-U SUSE / openSUSE 11 01-04-2007 01:57 AM
Can any one plz explain why/what for cron.d, cron.daily, cron.weekly etc are there. mavinashbabu Linux - Newbie 4 09-21-2006 01:50 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration