LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-29-2018, 03:56 AM   #1
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Rep: Reputation: 10
Post Crontab and update.


Hello.
On Ubuntu, I written below crontab:
Code:
00 6 * * 1 /usr/bin/apt-get update && /usr/bin/apt-get upgrade -y >>/home/update.log
And the "update.log" show me:
Code:
# tail update.log 
Need to get 569 kB/76.4 MB of archives.
After this operation, 202 kB of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 open-iscsi amd64 2.0.874-5ubuntu2.6 [280 kB]
Get:2 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 tmux amd64 2.6-3ubuntu0.1 [248 kB]
Get:3 http://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 unattended-upgrades all 1.1ubuntu1.18.04.8 [40.4 kB]
Fetched 569 kB in 2s (375 kB/s)
dpkg: warning: 'ldconfig' not found in PATH or not executable
dpkg: warning: 'start-stop-daemon' not found in PATH or not executable
dpkg: error: 2 expected programs not found in PATH or not executable
Note: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin
But when I update manually then I never get any error.
Any idea?

Thank you.
 
Old 12-29-2018, 05:09 AM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,163

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Quote:
Originally Posted by hack3rcon View Post
Code:
Note: root's PATH should usually contain /usr/local/sbin, /usr/sbin and /sbin
But when I update manually then I never get any error.
But when you work manually you're using (probably) the bash shell that will have set the superuser PATH.
In cron the default is /bin/sh and that will NOT execute your .bash_login cq .bashrc scripts (and might not even be bash at all, some distro's use dash as /bin/sh).
Code:
Cron jobs are always run under /bin/sh; see crontab(1) for more details.
Put the apt commands in a script, prefix that with
Code:
#!/bin/bash
and optionally even set the PATH, then run that script from cron so that you make sure it has the same environment as when you run it interactive.
 
1 members found this post helpful.
Old 12-29-2018, 07:51 AM   #3
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by ehartman View Post
But when you work manually you're using (probably) the bash shell that will have set the superuser PATH.
In cron the default is /bin/sh and that will NOT execute your .bash_login cq .bashrc scripts (and might not even be bash at all, some distro's use dash as /bin/sh).
Code:
Cron jobs are always run under /bin/sh; see crontab(1) for more details.
Put the apt commands in a script, prefix that with
Code:
#!/bin/bash
and optionally even set the PATH, then run that script from cron so that you make sure it has the same environment as when you run it interactive.
I created a .sh file with below content:
Code:
#!/bin/bash
/usr/bin/apt-get update && /usr/bin/apt-get upgrade -y
Then I edited crontab as below:
Code:
00 6 * * 1 /home/update.sh >>/home/update.log
Is it enough?

Last edited by hack3rcon; 12-29-2018 at 07:52 AM.
 
Old 12-29-2018, 09:59 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,570

Rep: Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335
No, if you run it from cron you need to set PATH too.
(but you would need 5 minutes to check it and you will see yourself)
 
Old 12-29-2018, 02:03 PM   #5
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by pan64 View Post
No, if you run it from cron you need to set PATH too.
(but you would need 5 minutes to check it and you will see yourself)
PATH? Which PATH?
 
Old 12-29-2018, 05:14 PM   #6
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,100

Rep: Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144
See http://man7.org/linux/man-pages/man7/environ.7.html

If you don’t know the PATH variable, you should not touch Linux servers. Or Windows, for that matter, which also has a PATH.
 
1 members found this post helpful.
Old 12-29-2018, 05:24 PM   #7
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.7.1908
Posts: 4,343

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Once again the OP is doing their help vampire/post-one-sentence-at-a-time thing because they are unwilling to learn how to use an internet search engine. Or man pages for that matter.

The link berndbausch posted is an excellent start. Also see man pages for
crontab(1)
cron(8)
 
Old 12-29-2018, 09:18 PM   #8
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,163

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Quote:
Originally Posted by hack3rcon View Post
Then I edited crontab as below:
Code:
00 6 * * 1 /home/update.sh >>/home/update.log
Is it enough?
I would add 2>*1 to that redirection, so
Code:
00 6 * * 1 /home/update.sh >>/home/update.log 2>&1
That will put any error messages into the log file too.

But yes, that should work provided the PATH is set correctly by the bash startup files.
 
Old 12-30-2018, 02:21 AM   #9
hack3rcon
Senior Member
 
Registered: Jan 2015
Posts: 1,432

Original Poster
Rep: Reputation: 10
Quote:
Originally Posted by ehartman View Post
I would add 2>*1 to that redirection, so
Code:
00 6 * * 1 /home/update.sh >>/home/update.log 2>&1
That will put any error messages into the log file too.

But yes, that should work provided the PATH is set correctly by the bash startup files.
PATH mean path of "sh" ?
Code:
00 6 * * 1 /bin/bash /home/update.sh >>/home/update.log 2>&1
 
Old 12-30-2018, 03:32 AM   #10
berndbausch
Senior Member
 
Registered: Nov 2013
Location: Tokyo
Distribution: A few
Posts: 4,100

Rep: Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144Reputation: 1144
Quote:
Originally Posted by hack3rcon View Post
PATH mean path of "sh" ?
Please do this:
1. Open a terminal window
2. Type man environ, followed by ENTER
3. Read about the shell environment, including the PATH
 
Old 12-30-2018, 03:43 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,570

Rep: Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335Reputation: 4335
Quote:
Originally Posted by hack3rcon View Post
PATH? Which PATH?
if you want to work with linux/unix: One of the very first things you have to learn is PATH. This is not optional, this is a must. Without PATH it looks like an [analog] clock without HANDLE. Useless.

Windows is not Linux, windows works differently and you must not expect any kind of similarity. And you need to be familiar with it if you want to use.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: What Is Crontab And How To Automate Tasks In Linux With Crontab LXer Syndicated Linux News 0 05-13-2017 06:45 PM
linux crontab vs unix crontab ytd Linux - General 2 08-09-2009 06:07 AM
replaced crontab, now should get crontab back to what it was raminn Linux - Newbie 2 10-20-2008 08:15 PM
man crontab(5) vs crontab(1) Canis Polaris Linux - Newbie 2 06-04-2008 05:03 PM
system-wide crontab in /etc/crontab ner Linux - General 2 11-18-2003 01:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:31 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