rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:03 pm

For the domoticz_backup.sh guy....

Is there a way to tell the crontab job to "run as you" the user, not as sudo or root?

User avatar
Paeryn
Posts: 2974
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:06 pm

I don't know if it's moved in Jessie (no reason to have), but in Wheezy reboot is in /sbin not /usr/bin

Code: Select all

pi@raspberrypi ~ $ which reboot
/sbin/reboot
She who travels light — forgot something.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:09 pm

Just thinking in text....

Where does the terminal run from when you just work in the terminal?
And how different is that from /usr/bin? ie If the files aren't there, it won't run

And then what is the crontab job running as?

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:12 pm

/sbin/reboot
under sudo crontab -e

testing



My Ubuntu machine is so slow it couldn't handle me remoting into it and then searching for terminal.... :roll:

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:13 pm

:o I see a restarting Ubuntu machine.... Thanks. 8-)

Now I have to get up again.... haha.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:17 pm

So dumb question for my Ubuntu machine....

Can I use apt-get update and apt-get upgrade under /usr/bin?

Is there an apt-get file or function under /usr/bin?


I can run sudo apt-get update under just the terminal.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:25 pm

Trying a sudo crontab -e
with
time /sbin/apt-get update
time /sbin/apt-get upgrade
time /sbin/reboot


Except.... I won't know if the apt-get's work. The reboot will work for sure since it worked before.


Which means I'll have to get up again in a few.... :roll:

But I won't know if the apt-get's worked still. Dang.

User avatar
Paeryn
Posts: 2974
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:26 pm

rasprm3 wrote:Just thinking in text....

Where does the terminal run from when you just work in the terminal?
The user's home directory e,g. /home/pi for the default user pi
rasprm3 wrote:And how different is that from /usr/bin? ie If the files aren't there, it won't run
/usr/bin is generally in the search path for commands, you can see which directories will be searched with echo $PATH

Code: Select all

pi@raspberrypi ~ $ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games
rasprm3 wrote:And then what is the crontab job running as?
It's generally best to use full path names in crontabs. On my RPi the PATH variable cron uses for the default user is

Code: Select all

/usr/bin:/bin
rasprm3 wrote:So dumb question for my Ubuntu machine....

Can I use apt-get update and apt-get upgrade under /usr/bin?

Is there an apt-get file or function under /usr/bin?

I can run sudo apt-get update under just the terminal.
apt-get is usually in /usr/bin/, you can always check where it is with

Code: Select all

which apt-get
She who travels light — forgot something.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:32 pm

Yep. Reboot worked. No surprise there.

I'll set the times for tomorrow on the sudo crontab -e apt-get jobs Using /sbin

Maybe I'll luck out and they'll run that way. As long as they run. That's good enough.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:41 pm

On my Ubuntu machine, my apt-get is /usr/bin. Hm.

Can someone remind me how to get around on a Linux command line please?
I need the equivalent to Windows...
cd\
dir
cd


And then something for type

I could spit out a log somewhere. Use >> or >, depending on if I want the previous run info. Then use type to see it over command line. Is there "type" for Linux?

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:43 pm

ls
is equal to the Windows dir?

http://linuxcommand.org/lc3_lts0060.php

These are bash commands? The Linux command line is a bash command line? Or at least in ubuntu....

If so, I'm used to this page.
http://ss64.com/bash/

apt-get is in there.... Nice find here. I like ss64.com for details on Windows command line options.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:49 pm

Nice.

cd .. to go up/back a level in the folders. Now I can get around.


For some reason, under my localadmin account on the Ubuntu machine,
I'm in the home folder, then this localadmin folder I think....
it's got a list of blue "folders" but I can cd into them.

They either must not exist or I don't have permission on them, but they're in my own localadmin home folder. I think.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:51 pm

Looks like type can type a file.

http://ss64.com/bash/type.html

I just don't have a file to try it out on. Although I bet I could get something to spit out for a log file now. I don't know what a .txt file equivalent is here.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:54 pm

Interesting and might be relevant....


sudo apt-get update > /usr/bin/test.log

That gives me a response of bash: /usr/bin/test.log: Permission denied

sudo apt-get update asks for my password, ready to run.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:56 pm

I just need to get it to spit a log file out I think.

I did sudo apt-get update and it runs and processes something. It's enough that I think it's updating something. Even though I already did updates before, it looks like it picked something new up.

A log file.... with the date stamped in it or on it. That should tell me if it's running and give me some feedback on what's going on.

User avatar
Paeryn
Posts: 2974
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 10:59 pm

The RPi uses bash as it's primary shell. Although for compatability reasons some things like cron might default to sh. For basic usage they are the same but there are differences when writing shell scripts.

cat displays a file, though if it's long you will want to use something like less which is a text viewer.

The default user doesn't have permission to write to /usr/bin (or many places other than in directories within their home directory) and you shouldn't be trying to write log files there.
Last edited by Paeryn on Thu Dec 17, 2015 11:02 pm, edited 1 time in total.
She who travels light — forgot something.

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Thu Dec 17, 2015 11:02 pm

Hm. I don't get it.

I do ls on my localadmin.... /home/localadmin I think.
ls shows Desktop..... Oh darn.... Linux.... Recognizes capital letters..... :o :D :lol:

cd Desktop works. :roll:


And I just create my first log file....
on terminal
sudo apt-get update >> /home/localadminaccount/Desktop/test.log

Ubuntu has the gui, so I just clicked on the file and saw the text guts. It worked. Very nice.

asandford
Posts: 1998
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 1:53 am

rasprm3 wrote: sudo apt-get update > /usr/bin/test.log

That gives me a response of bash: /usr/bin/test.log: Permission denied
Writing logs to any /.../bin directory is very bad idea as you run the risk of overwriting crucial files. [the ... is a notation that represents any number of characters, so here it means any 'bin' directory (/bin, /usr/bin, /usr/local/bin, etc)]

Consider this:

Code: Select all

DO NOT RUN THIS CODE
$sudo su -
#/bin/true > /bin/true .log
#ls -l /bin/true
-rwxr-xr-x 1 root root 0 Dec 18 01:31 /bin/true
DO NOT RUN THIS CODE
As you can see, I've just zapped my true command because I added a space in the logfile name by mistake!

(no system commands were harmed in this demonstration, /bin/true was restored from a copy)

The reason you got permission denied is that apt-get runs as root, but the redirection (>) runs as you, and you don't have permission to write files into the /usr/bin directory.

User avatar
rpdom
Posts: 17217
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 3:47 am

rasprm3 wrote:Hm. I don't get it.

I do ls on my localadmin.... /home/localadmin I think.
ls shows Desktop..... Oh darn.... Linux.... Recognizes capital letters..... :o :D :lol:

cd Desktop works. :roll:


And I just create my first log file....
on terminal
sudo apt-get update >> /home/localadminaccount/Desktop/test.log

Ubuntu has the gui, so I just clicked on the file and saw the text guts. It worked. Very nice.
Some handy Bash hints for you:

The directory "~" is the same as /home/$LOGNAME (/home/localadminaccount for you). Also the same as $HOME.
You could write your last line as any of these

Code: Select all

sudo apt-get update >> /home/localadminaccount/Desktop/test.log
sudo apt-get update >> ~/Desktop/test.log
sudo apt-get update >> $HOME/Desktop/test.log
Just "cd" without any arguments will take you to your home directory.
"cd -" will take you back to the last directory visited.
"pwd" will show you what your current location is. That is also available in the PWD shell variable. echo $PWD

rjobaan
Posts: 53
Joined: Sun Aug 18, 2013 1:07 pm

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 10:32 am

My cronjob is working now
You have to crontab
one Sudo crontab -e en one crontab -e

It's working form me in the crontab -e
Don't forget to set the permission correct
Sudo chmod 777 filename.sh

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 10:40 am

rasprm3 wrote:Interesting and might be relevant....


sudo apt-get update > /usr/bin/test.log

....
does not do what you think
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
RaTTuS
Posts: 10559
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 10:45 am

to update and upgrade then reboot
you would do it from
sudo crontab -e
then have
[time] /full/path/to/apt-get update && /full/path/to/apt-get upgrade -y && /full/path/to/reboot

so it will reboot if upgrade works
and it will upgrade if update works

if you have them as
[time] command one
[time] command two
[time] reboot
you will end up with a cabbaged system as the reboot will happen before the others have finished

look in
dmesg
or
cat /var/log/syslog
for when things happen

don't try to run before you can walk

putting reboot commands in crontab will lead to tears

reboot sections in crontab do thing when the system has rebooted
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 3:21 pm

Ah, new posts. Popping in to say thanks for the basic LInux cmd line commands. That's my level for now.

Glancing above I see what I was thinking and just tested....

update && reboot

I got /sbin/reboot to run yesterday... so the cronjob ran.

This test for update&&reboot didn't reboot the Ubuntu machine though. So it's either not running (I bet it is) or something's not updating probably..... So the wrong folder or permissions maybe....

rasprm3
Posts: 195
Joined: Sun Jan 20, 2013 1:27 am

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 10:19 pm

Got some more info off my Pi at lunch. This might be interesting.

I use Putty, so this is on a terminal. On a Pi.

I typed sudo apt-get update && apt-get upgrade -y

That did the update, but then I got permission errors for the upgrade part.
(After that I did sudo apt-get update && sudo apt-get upgrade -y and it ran fine, like I wanted. I had to put the extra sudo in.)

Errors from the first attempt:

Reading package list.... Done
E: Could not open lock file /var/lib/dpkg/locl - open (13: Permission denied)
E: Unable to lock the adminstration directory (/var/lib/dpkg/), are you root?

DirkS
Posts: 10367
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Cronjob / Linux command line question

Fri Dec 18, 2015 10:38 pm

rasprm3 wrote:(After that I did sudo apt-get update && sudo apt-get upgrade -y and it ran fine, like I wanted. I had to put the extra sudo in.)
It's not an extra sudo. These are two separate commands that run one after the other.
The '&&' means that the second command only runs if the first one was successful (see e.g. http://unix.stackexchange.com/questions ... s-with-and)

Return to “Beginners”