LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Crontab is not working properly on CentOS 6.3 (https://www.linuxquestions.org/questions/linux-newbie-8/crontab-is-not-working-properly-on-centos-6-3-a-4175429653/)

miguelangeljma 09-29-2012 06:06 PM

Crontab is not working properly on CentOS 6.3
 
Hello, I am trying to schedule to run a script I made using Crontab. The problem is that although I use the right syntax of the command, it is doing NOTHING, and it is getting very annoying.

All i want is the script to run in a new terminal window. When i run the script manually it works perfect. What i am doing is the following:

crontab -e

*/1 * * * * bash /home/user/Script.sh

I also added the dir /home/user to the PATH so i can just write Script.sh to run the script. nothing has worked so far, not even the "at" Command.

Please help me, i'm new using Linux and got tired of the "Crash-Stress-meter" of Windows, thanks in advance.

KinnowGrower 09-29-2012 07:45 PM

Quote:

Originally Posted by miguelangeljma (Post 4792799)
*/1 * * * * bash /home/user/Script.sh

you can try to replace "bash" with root if script is supposed to run as root. I think bash is not a right option. Also not that S is capital in name of script.sh. I am assuming that is correct

miguelangeljma 09-30-2012 01:00 AM

Quote:

Originally Posted by KinnowGrower (Post 4792841)
you can try to replace "bash" with root if script is supposed to run as root. I think bash is not a right option. Also not that S is capital in name of script.sh. I am assuming that is correct

It is not supposed to run as root, i am just testing the command to then apply it to automatic backups etc. Yes, the name of the script file is on capital S "Script.sh".

suicidaleggroll 09-30-2012 02:00 AM

* Remove the bash in the call, and add a #!/bin/bash to the top of the script if that's what it's supposed to run in.

* Make sure there's a blank line in the crontab after the command you plan to run

* Your PATH makes no difference in commands running through cron, as they don't source your .bashrc or .bash_profile. Cron's PATH is and will always be very limited, so make sure to reference the full path to any commands you call in the script

* Run your script with > /home/user/script.out 2>&1 at the end so you can see the output of the script, IE:
Code:

*/1 * * * * /home/user/Script.sh > /home/user/script.out 2>&1
* Post the contents of Script.sh

miguelangeljma 09-30-2012 10:54 AM

Quote:

Originally Posted by suicidaleggroll (Post 4793027)
* Remove the bash in the call, and add a #!/bin/bash to the top of the script if that's what it's supposed to run in.

* Make sure there's a blank line in the crontab after the command you plan to run

* Your PATH makes no difference in commands running through cron, as they don't source your .bashrc or .bash_profile. Cron's PATH is and will always be very limited, so make sure to reference the full path to any commands you call in the script

* Run your script with > /home/user/script.out 2>&1 at the end so you can see the output of the script, IE:
Code:

*/1 * * * * /home/user/Script.sh > /home/user/script.out 2>&1
* Post the contents of Script.sh

Hello, i made a video of what i'm doing so you can tell me what i'm doing wrong, Hope my bad english is understandable and the video shows everything you guys need to help me out. Thanks again for your efforts.

This is the link of the video http://www.youtube.com/watch?v=9LiTR...ature=youtu.be

repo 09-30-2012 11:00 AM

Is the cron daemon running?
Create a simple script, to see if that works.
Post the content of Script.sh

Kind regards

unSpawn 09-30-2012 11:22 AM

...additionally
Quote:

Originally Posted by miguelangeljma (Post 4792799)
All i want is the script to run in a new terminal window.

cron jobs are usually meant to perform some automated task requiring no user input or access to the desktop, so if your cron job is supposed to launch some GUI tool then you might have to explicitly export the DISPLAY variable in your script. Also note that if the cron daemon is running and unless you explicitly unset the MAILTO variable, any error messages will by default be sent to the crontab owner so check your local mailbox too.

miguelangeljma 09-30-2012 12:14 PM

Quote:

Originally Posted by unSpawn (Post 4793255)
...additionally

cron jobs are usually meant to perform some automated task requiring no user input or access to the desktop, so if your cron job is supposed to launch some GUI tool then you might have to explicitly export the DISPLAY variable in your script. Also note that if the cron daemon is running and unless you explicitly unset the MAILTO variable, any error messages will by default be sent to the crontab owner so check your local mailbox too.

i tested scheduling crontab to ping www.google.com to see if it was performing that action at the time i set it up, IT WAS :) so crontab is working, but not the way i need.

I am very sorry for being such a pain in the a** but i need to ask, how do i export the display in my script, so that when crontab executes my script it will pop up a new terminal window with the content of the script on it (allowing the user to interact with the executed script)?

Thanks in advance

unSpawn 09-30-2012 12:50 PM

Since you haven't been methodical about things and haven't answered all questions in this thread (which you should have)

as root please run:
Code:

/sbin/service crond status
rpm -qa|grep -i cron

That way we can see if which cron(-related) SW is installed and if the cron daemon is running.

Now as unprivileged user (your own account) please run:
Code:

crontab -l
cat /home/user/Script.sh


miguelangeljma 09-30-2012 03:48 PM

Code:

[Miguel@Miguel Desktop]$ su -
Password:
[root@Miguel ~]# /sbin/service crond status
crond (pid  2613) is running...
[root@Miguel ~]# exit
logout

I deleted the cron jobs i previously had, now I know that cron is working fine.

Code:

[Miguel@Miguel Desktop]$ crontab -l
no crontab for Miguel

And this is the script i am trying to execute in a new terminal window (xterm terminal window) using crontab or any other tool that can perform what i want.

Code:

[Miguel@Miguel Desktop]$ cat /home/Miguel/Script_Calculadora.sh
#!/bin/bash
 
export DISPLAY=:0.0
clear
opcion=400
while [ $opcion -ne 5 ]
do

echo "Bienvenidos a la calculadora basica, por favor elija una opcion "
echo "1.Suma"
echo "2.Resta"
echo "3.Multiplicacion"
echo "4.Division"
echo "5.Salir"

echo "Introduce una opcion "
read opcion
case $opcion in

1) echo "Introduce el primer numero "
read a
echo "Introduce el segundo numero "
read b
echo "El resultado es '$(( $a + $b ))'" ;;

2) echo "Introduce el primer numero "
read a
echo "Introduce el segundo numero "
read b
echo "El resultado es '$(( $a - $b ))'" ;;

3) echo "Introduce el primer numero "
read a
echo "Introduce el segundo numero "
read b
echo "El resultado es '$(( $a * $b ))'" ;;

4) echo "Introduce el primer numero "
read a
echo "Introduce el segundo numero "
read b
echo "El resultado es '$(( $a / $b ))'" ;;
esac
done


unSpawn 09-30-2012 04:47 PM

Like I said before cron jobs are meant to perform automated tasks requiring no user input or access to the desktop. In your case exporting the DISPLAY variable has to be done as crond executes the job. Here is an example cron job that will fire (as long as the env and zenity binaries are available, in the path cron knows about and there is actually a display running at ":0.0") each day at 9 PM:
Code:

0 21 * * * env DISPLAY=":0.0" zenity --title "Ta-daa" --info --text "Hello World\!"

miguelangeljma 09-30-2012 11:25 PM

Quote:

Originally Posted by unSpawn (Post 4793454)
Like I said before cron jobs are meant to perform automated tasks requiring no user input or access to the desktop. In your case exporting the DISPLAY variable has to be done as crond executes the job. Here is an example cron job that will fire (as long as the env and zenity binaries are available, in the path cron knows about and there is actually a display running at ":0.0") each day at 9 PM:
Code:

0 21 * * * env DISPLAY=":0.0" zenity --title "Ta-daa" --info --text "Hello World\!"

Thanks, this example was very helpful, now i understand that crontab won't give me any user interaction pop-up.

I was testing with the zenity command along with the xdg-open command to open my script and it is kinda working, but instead of opening my script on a new terminal window it was opening it on a text editor software.

Since it is not completely related to the main thread (Crontab is not working) we can close it, unless you can give me an answer to my 2nd question before closing it. What i need is to open a script as an executable file from the command line, i want it to be executed the same way i run it from the command line, but in a different command line.

Tinkster 09-30-2012 11:56 PM

Try something like this, maybe?

Code:

55  16  * * * DISPLAY=:0.0 xterm -e "/home/Miguel/Script_Calculadora.sh"


All times are GMT -5. The time now is 01:37 PM.