Problem with command cron in SUSE

Hi everybody, i’m new at this community!
I’m having a problem with the command cron in suse
I’m also learning bash so here is the question… i want to try this command, with a basic script:

i want to use cron, for a date (example now: 17:10 PM on 17/6/2015, minute 7), that execute a “Hello World” and sabe it on another file

I think is something like this, but is not working at all.

On crontab file i put

07 17 17 6 * echo ‘hello’ > /etc/logcron.txt
07 17 17 6 * /root/user/pruebacron.txt (here pruebacron is a script that includes echo ‘hello’ > /etc/logcron.txt)
07 17 17 6 * echo ‘hello’ | > /etc/logcron.txt (here i included pipe… to send the answer to the file…)

but neither of the alternatives worked fine… maybe im confused with sintaxis… i hope u can help me

thank you very much!
licha!

Hi
By default cron has no environment set, in either cron or your scripts need the full path to the command.

You can find this with the which command, or in your script use which…



crontab
/usr/bin/echo "Hello from echo" > /tmp/mycronjob.txt"

Script
#!/bin/bash
MYECHO=`/usr/bin/which echo`
$MYECHO "A new Hello from echo" > /tmp/mycronjob.txt

You can also set the paths environment variable in the crontab…

You should also look at putting stuff into /tmp and use some other script or method to check it… if your task accidentally removes files, better in /tmp :wink:

On 2015-06-17 22:16, licha lop wrote:

> On crontab file i put

What crontab file exactly? There are many.

Normally you would, as user, run “crontab -e”, per the manual. If you
don’t like the default editor, change the EDITOR variable.

There are also tools in kde to edit the user crontab, I don’t remember
the name.

> 07 17 17 6 * echo ‘hello’ > /etc/logcron.txt

Does the user that run this have permissions to write there?

> 07 17 17 6 * /root/user/pruebacron.txt (here pruebacron is a script that
> includes echo ‘hello’ > /etc/logcron.txt)

Normally you don’t name a script as .txt, although Linux does not care.
It has to have executable permissions, though. And the directory you
have used is also strange, although it does not matter, provided the job
runs as root. Which makes me think that you are running as root, which
is something you should never do.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

Hey, thanks.
But there, where do u set the time? On the crontab?
The crontab i was talking about is the one wich is in /etc/crontab
It would be:

crontab
Minute Hour … … … /usr/bin/echo “Hello from echo” > /tmp/mycronjob.txt" ?
Do i have to create before the mycronjob.txt? Or it creates automatically.
The idea in this case would be to see on that txt lines saying “Hello from echo” all the time the job is executed? Correct?

And if i want to crontab execute another script, would be with both commands?
Script
#!/bin/bash
MYECHO=/usr/bin/which echo
$MYECHO “A new Hello from echo” > /tmp/mycronjob.txt

And the one that goes on the crontab file? (without the > /tmp/mycronjob.txt because im doing that on my script)
XX XX XX XX XX /usr/bin/echo scriptBefore

Thanks… i hope i understood… jaja

the user have permissions.
good to know about the txt… don’t know why i put that extension haha! but as you said, it doesn’t affect.
thanks!

On Wed 17 Jun 2015 11:46:01 PM CDT, licha lop wrote:

malcolmlewis;2715657 Wrote:
> Hi
> By default cron has no environment set, in either cron or your scripts
> need the full path to the command.
>
> You can find this with the which command, or in your script use
> which…
>
> >
Code:

> >
>
> crontab
> /usr/bin/echo “Hello from echo” > /tmp/mycronjob.txt"
>
> Script
> #!/bin/bash
> MYECHO=/usr/bin/which echo
> $MYECHO “A new Hello from echo” > /tmp/mycronjob.txt
>

> >
>
> You can also set the paths environment variable in the crontab…
>
> You should also look at putting stuff into /tmp and use some other
> script or method to check it… if your task accidentally removes
> files, better in /tmp :wink:

Hey, thanks.
But there, where do u set the time? On the crontab?
The crontab i was talking about is the one wich is in /etc/crontab
It would be:

crontab
Minute Hour … … … /usr/bin/echo “Hello from echo” >
/tmp/mycronjob.txt" ?
Do i have to create before the mycronjob.txt? Or it creates
automatically.
The idea in this case would be to see on that txt lines saying “Hello
from echo” all the time the job is executed? Correct?

And if i want to crontab execute another script, would be with both
commands?
Script
#!/bin/bash
MYECHO=/usr/bin/which echo
$MYECHO “A new Hello from echo” > /tmp/mycronjob.txt

And the one that goes on the crontab file? (without the >
/tmp/mycronjob.txt because im doing that on my script)
XX XX XX XX XX /usr/bin/echo scriptBefore

Thanks… i hope i understood… jaja

Hi
For a user crobjob you don’t do anything in /etc, just as the user run
the command;


crontab -e

Then you enter the time etc your wanting it to occur.

To see what is present in the user crontab use;


crontab -l

You can have one script that will do everything, lots of little scripts
doing different things. It all depends on what your wanting to do…

For system jobs, you just place the executable
script in cron.daily .hourly .weekly and .monthly.

You can also use systemd timers if wanting jobs to occur at less than 1
minute.

Perhaps detailing exactly what your wanting to do and timing etc might
be better so users can offer advice…


Cheers Malcolm °¿° LFCS, SUSE Knowledge Partner (Linux Counter #276890)
SUSE Linux Enterprise Desktop 12 GNOME 3.10.1 Kernel
3.12.43-52.6-default If you find this post helpful and are logged into
the web interface, please show your appreciation and click on the star
below… Thanks!

Hey, thank u very much! Tomorrow i’ll try what u have told me.
But basically i want that every day at 17:00PM, a file (script) located in another folder (ej: /home/myfolder/script), and also HAVE a log that this task WAS executed (this log could be located on that direction un tmp).

Now im trying it with something easier: a script that only says “Hello World”. Cron that script, and log it. That would me the task.

Thanks

On 2015-06-18 06:06, licha lop wrote:

> Hey, thank u very much! Tomorrow i’ll try what u have told me.
> But basically i want that every day at 17:00PM, a file (script) located
> in another folder (ej: /home/myfolder/script), and also HAVE a log that
> this task WAS executed (this log could be located on that direction un
> tmp).

Just start by running “crontab -e” as the user that you want the cron
job to run, and type the sequence…


33 22 * * *   /home/cer/bin/darlavara >> /home/cer/bin/darlavara.log

It is that simple.


Cheers / Saludos,

Carlos E. R.
(from 13.1 x86_64 “Bottle” at Telcontar)

Hi,

Just a heads up, the default editor in openSUSE is vim, so unless you know how to use vim you don’t have to worry about a thing when editing that crontab file.
That being said you can change the default editor (temporary)

EDITOR=foo; export EDITOR
crontab -e

Of course you need to change foo into the file editor you know :wink:

A permanent change would be editing

~/.profile

for a single user

or

/etc/profile.local

for system wide changes.

Note I don’t use ze C shell nor its variants since some folks say it does not honor *.profile but that is another story :slight_smile:

Well, i’am here again.
It worked. This is what i did

Script


#!/bin/bash
echo 'Hello World'
echo 'Save It' >> /root/user/log.txt

In crontab -e

22 10 18 6 * /root/user/script.sh

Now, what only remains, **is to open my browser **(mozzilla in this case) from my crontab
In my crontab -e, at the end i have put

localhost/script_exec.php?data_sources=20

But the browser doesn’t appear at that time. I think it’s doing nothing.
Maybe to opening a php page, i have to put another command that i don’t know… so may be you can help me with that too!

Thanks to all!!!

Making it clear

If on console I put:


Firefox localhost/.....exec.php?data_soruces=1

The browser appears, on that page, everything OK

If i put the SAME sentence in the cron:


25 10 17 6 * Firefox localhost/....exec.php?data_sources=1

No browser appears. Don’t know if it’s executing that task or not.

It uses Apache.

Thanks!

On Thu 18 Jun 2015 03:06:01 PM CDT, licha lop wrote:

Making it clear

If on console I put:

Code:

Firefox localhost/…exec.php?data_soruces=1

The browser appears, on that page, everything OK

If i put the SAME sentence in the cron:

Code:

25 10 17 6 * Firefox localhost/…exec.php?data_sources=1

No browser appears. Don’t know if it’s executing that task or not.

It uses Apache.

Thanks!

Hi
Cron is a text (console) tool it knows nothing of X applications. So
you need to tell firefox which display to use (see man firefox).

What happens if X isn’t running?


Cheers Malcolm °¿° LFCS, SUSE Knowledge Partner (Linux Counter #276890)
SUSE Linux Enterprise Desktop 12 GNOME 3.10.1 Kernel
3.12.43-52.6-default If you find this post helpful and are logged into
the web interface, please show your appreciation and click on the star
below… Thanks!

What is the program Firefox? When I try to run that as a command:

henk@boven:~> Firefox
If 'Firefox' is not a typo you can use command-not-found to lookup the package that contains it, like this:
    cnf Firefox
henk@boven:~>

It may be that youy mean firefox. But please, copy/paste from yopur terminal window into a post withou changing things. Else wee will start not to believe anything you post.

And when you start something from cron, it is in the background. It has no connection to any of the GUI sessions that might or might not run at the same time.

execute it firefox (not Firefox)

Yeah, im searching and i know it’s something about the “DISPLAY” but don’t know how it Works…

As i said, literally:

On console


firefox localhost/synchro/synchro_exec.php?data=sources=1

It opens OK

On crontab (I WANT: AT A SPECIFIC TIME, EXECUTE THIS TASK, THAT OPEN THE BROWSER ON THAT PAGE)


40 12 18 6 * firefox localhost/synchro/synchro_exec.php?data=sources=1

Here nothing happend. When i go to the logs, appears the “DISPLAY” thing. I tried to put 0.0 as i searched but it was the same…

Thanks!

PD: I tried to do a script, that execute this sentence and it Works (executing ./script). It opens the browser. When it doesn’t work is when i put the completely code (firefox…localhost…) on the crontab or even if i put the path script that executes it. The problem is the cron! :stuck_out_tongue:

In command line if i put whoami and the display, it says 0.0 root. So i dont understand…

cron executes in text mode you have to force a GUI to the screen context that you want it to start on. Also be aware that threads make a difference here too . cron is executing in a completely different thread then the GUI system.

Cron is not really designed to periodical start a GUI app. It is made to run scripts for maintenance such as backup or clean up chores.

Maybe if you told us what you want to actually accomplish there well may be an easier way to do it.

You should try to understand the nature of a Unix/Linux operating system. At any moment in time, there can be many users loged in , either local or remote. When you then start in the background a program that wants to open a window, on which of those user sessions should it try to do that?

It is even possible that there is no GUI session open at all. When you log out and all other users log out and firefox starts in the background, where should it try to open that window?

Thus the minimum required is that you define the X display that window should be opened on. You using a PC, that is most probably the X display on that PC itself and that is localhost:0.0 (try to find information on what that means) or short :0.0. That information is communicated to the program (firefox in this case) by defining it in the process environment variable DISPLAY. On the coomand line you can do this with

DISPLAY=":0.0" firefox ........

Now firefox will try to open it’s window on that X display. But that can only succeed if somebody is loged in there. And also if that somebody allows this (imagine when everybody could open windows on your session from outside it at will, not only annoying,but also one could read everyting you type). The most simple way to do this is

xhost +

??? When I checked this, it seems that xhost is not installed by default, thus it could be that you must install it first. Or is it depricated and is there a newer replacement???

This xhost+ command should of course be executed from your GUI session because that is the place you want to allow another one to open windows. Most easy is that to put it in your ~/.profile.

Hi
OK, what I think you are going to do is break the task into two steps…

For a start, are you running your desktop as root (not a good thing…) it should be as your user, please confirm.

So, what you need to do is run some script via cron as root user and update a php page, so set that up and in the script at the begining pipe out something as a flag to /tmp that the job is running and at the end pipe out something out as a flag to say it’s complete and remove the running flag.

Then as you user (running the GUI) you could setup a job (script) to watch (as in the watch command) for the file to be created and then start firefox with the local file url.

Hey! Thanks x the answers, im understanding… Now i see the difference between executing it from console and from cron (don’t have GUI here, is all background)…

This would be the question…

I’m not watching the browser appearence but the task should run if a write

          • /localhost/synchro/synchro_exec.php?data_soruces=1
            ??

**
To update you mean writing the sentence above? THAT’S only i need. UPDATE. Watching or not the browser, is not important** :stuck_out_tongue:

Would be OK to run just /localhost/…php… on a crontab? Don’t i need to specify that the referenced file (PHP) “opens” with a browser (default)?
(Removing the Word Firefox on crontab)

Im root. This are test.

Thanks

Never ever run a GUI logged in as root. Easy to breakthings without even trying.

You can be logged into a console or terminal as root but never ever a GUI, even for testing;)

On 2015-06-18 18:36, licha lop wrote:

> Im root. This are test.

Don’t.


Cheers / Saludos,

Carlos E. R.

(from 13.1 x86_64 “Bottle” (Minas Tirith))