LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab and update. (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-and-update-4175645155/)

hack3rcon 12-29-2018 02:56 AM

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.

ehartman 12-29-2018 04:09 AM

Quote:

Originally Posted by hack3rcon (Post 5942413)
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.

hack3rcon 12-29-2018 06:51 AM

Quote:

Originally Posted by ehartman (Post 5942426)
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?

pan64 12-29-2018 08:59 AM

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)

hack3rcon 12-29-2018 01:03 PM

Quote:

Originally Posted by pan64 (Post 5942501)
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?

berndbausch 12-29-2018 04:14 PM

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.

scasey 12-29-2018 04:24 PM

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)

ehartman 12-29-2018 08:18 PM

Quote:

Originally Posted by hack3rcon (Post 5942467)
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.

hack3rcon 12-30-2018 01:21 AM

Quote:

Originally Posted by ehartman (Post 5942684)
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

berndbausch 12-30-2018 02:32 AM

Quote:

Originally Posted by hack3rcon (Post 5942728)
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

pan64 12-30-2018 02:43 AM

Quote:

Originally Posted by hack3rcon (Post 5942570)
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.


All times are GMT -5. The time now is 12:54 PM.