LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-17-2011, 10:00 AM   #1
rivacom
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Rep: Reputation: 0
Bash script works in terminal but not in cron.


I'm really running into a wall trying to figure this out. I have a Bash script and narrowed down the one command that doesn't seem to work via cron and it's my pgp decrypting line. Works fine if I run the command via terminal but if I run it via cron it doesn't output anything.

crontab -e shows the cronjob and it runs, creates the log file with no output. Is there maybe something I need to run as well? Permissions look set, unless the cron is running as a different user(I was under the assumption if it showed up under crontab while logged into that user, then it would run as that user.
 
Old 02-17-2011, 10:10 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi,

If you run a script from cron make sure the script doesn't use any environment variables. Those variables are present when you are logged in as user X, but are not present when running from cron.

You might want to post the part you think isn't working so we can have a look.

Hope this helps.
 
Old 02-17-2011, 10:11 AM   #3
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
In the user's crontab you can also define necessary environment variables and a different PATH. Maybe something is missing.
 
Old 02-17-2011, 10:27 AM   #4
jsanchez2004
LQ Newbie
 
Registered: Oct 2009
Posts: 2

Rep: Reputation: 0
you can launch the script with
#!/bin/bash -l

which adds all the user env vars or

launching it from crontab or script as follows :

su USER -c " COMMAND "
 
Old 02-17-2011, 11:43 AM   #5
rivacom
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
So this is my code below. I tried the su USER -c with no luck as well.

Code:
#! /bin/bash -l
clear
gpg --passphrase password -d /home/user/Scripts/file.txt.pgp > /home/user/Scripts/log.txt
exit
My cron is

11 35 * * * /home/user/Scripts/test
 
Old 02-17-2011, 11:55 AM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by rivacom View Post
Code:
11 35 * * * /home/user/Scripts/test
Shouldn't it be:
Code:
35 11 * * * /home/user/Scripts/test
 
Old 02-17-2011, 12:03 PM   #7
rivacom
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Sorry, i just wrote it backwards, it is 35 11. but the cron is running, it's just not running right.
 
Old 02-17-2011, 12:07 PM   #8
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
Ok. Do you get any email about the failed cron job?
 
Old 02-17-2011, 12:09 PM   #9
rivacom
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Like I said, the job works as the log.txt is created. But it's blank. However, when I run it in terminal the file gets created but the output is there.
 
Old 02-17-2011, 12:11 PM   #10
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
You checked that there is no email? cron will send an email for failed jobs.
 
Old 02-17-2011, 01:58 PM   #11
rivacom
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
where would it email to?
 
Old 02-17-2011, 02:00 PM   #12
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.0
Posts: 1,336

Rep: Reputation: 255Reputation: 255Reputation: 255
The local user in the machine. Just type mail. It could also be defined in your crontab to send it to a different user or none at all.
 
Old 02-17-2011, 02:26 PM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393Reputation: 2393
Hi,

gpg sends output to screen which might be a problem when there is no /dev/tty to write to. Add the --no-tty option to the gpg line.

gpg --no-tty --passphrase password -d /home/user/Scripts/file.txt.pgp > /home/user/Scripts/log.txt

Your script also has a command that clears the terminal, but there is no terminal when run from cron, this would be better:
Code:
#!/bin/bash
gpg --no-tty --passphrase password -d /home/user/Scripts/file.txt.pgp > /home/user/Scripts/log.txt
exit 0
BTW: This isn't too smart: It shows your passphrase, which can be read by root and thus give root the possibility to read your encrypted stuff (which root cannot without the passphrase).

Anyway, hope this helps.
 
Old 02-18-2011, 01:30 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,682

Rep: Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490Reputation: 2490
I also wouldn't assume gpg is always going to be in the $PATH in cron.
 
Old 02-18-2011, 08:11 AM   #15
rivacom
LQ Newbie
 
Registered: Jul 2010
Posts: 19

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by druuna View Post
Hi,

gpg sends output to screen which might be a problem when there is no /dev/tty to write to. Add the --no-tty option to the gpg line.

gpg --no-tty --passphrase password -d /home/user/Scripts/file.txt.pgp > /home/user/Scripts/log.txt

Your script also has a command that clears the terminal, but there is no terminal when run from cron, this would be better:
Code:
#!/bin/bash
gpg --no-tty --passphrase password -d /home/user/Scripts/file.txt.pgp > /home/user/Scripts/log.txt
exit 0
BTW: This isn't too smart: It shows your passphrase, which can be read by root and thus give root the possibility to read your encrypted stuff (which root cannot without the passphrase).

Anyway, hope this helps.
Would you recommend a better way? I tried probably for 3 days trying to read text output to determine when to send a password response, nothing ever worked. This was the only way for the password to be sent that worked for me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script works when run but not when called by cron grob115 Linux - Server 7 08-28-2010 12:24 PM
Bash script works from command line, fails from cron cmfarley19 Linux - General 4 08-14-2009 01:24 PM
Keep getting errors when running cron job that works in normal terminal. Techno Guy Linux - Newbie 6 05-31-2009 07:48 PM
Shell Script works different from cron necrolin Programming 2 05-07-2009 10:52 AM
bash script works when interactive, endless loop when started via cron dguy Linux - General 5 04-10-2006 12:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:38 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration