How to execute shell script via crontab?












22















I have a notify.sh script that looks like:



notify-send "hi welcome"


My crontab notification for 2 PM:



0 14 * * * home/hacks/notify.sh


However, this doesn't work. What is the problem?










share|improve this question





























    22















    I have a notify.sh script that looks like:



    notify-send "hi welcome"


    My crontab notification for 2 PM:



    0 14 * * * home/hacks/notify.sh


    However, this doesn't work. What is the problem?










    share|improve this question



























      22












      22








      22


      7






      I have a notify.sh script that looks like:



      notify-send "hi welcome"


      My crontab notification for 2 PM:



      0 14 * * * home/hacks/notify.sh


      However, this doesn't work. What is the problem?










      share|improve this question
















      I have a notify.sh script that looks like:



      notify-send "hi welcome"


      My crontab notification for 2 PM:



      0 14 * * * home/hacks/notify.sh


      However, this doesn't work. What is the problem?







      linux crontab






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 24 '13 at 20:24









      slhck

      160k47445467




      160k47445467










      asked Dec 10 '09 at 8:38









      AravindAravind

      291138




      291138






















          7 Answers
          7






          active

          oldest

          votes


















          38














          Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script.



          Make it look like this:



          #!/bin/sh
          notify-send "hi welcome"


          and make sure the script is executable:



          ls -l home/hacks/notify.sh
          chmod +x home/hacks/notify.sh
          ls -l home/hacks/notify.sh


          Also, since you're asking for this to happen just once a day, is the timezone of the crontab the same as your own timezone? You might find this happening at 2pm GMT.






          share|improve this answer
























          • +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

            – FXQuantTrader
            Jan 9 '15 at 6:10











          • Very subtle explanation. My upvote

            – Fokwa Best
            Oct 24 '16 at 12:58



















          18














          Making crontab running is easy only . Here I am going to say how to run crontab jobs. It is useful for anyone who is stuck on crontab.



          */1 * * * * cd /home/hacks && sh notify.sh


          To make the script executable, we have to do:



          chmod +x home/hacks/notify.sh


          Here i run this script for every one minute ...
          By doing below script, you can write it in a log file to find whether its working



          write log



          */1 * * * * cd /home/hacks && sh notify.sh>>test.log


          send mail



          */1 * * * * cd /home/hacks && sh notify.sh>>test.log | mail -s "Hi this is example" user@domain.com





          share|improve this answer





















          • 2





            isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

            – user1179459
            Sep 25 '15 at 1:14



















          5














          4 hypothesis:




          • the cron daemon is not running (do a ps axfww | grep cron and check)


          • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.


          • your script is not executable


          • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path







          share|improve this answer

































            1














            Add export DISPLAY=:0 above the notify-send line in your script. This addresses lornezog's second point.






            share|improve this answer































              0














              You have to open crontab by the following command:



              crontab -u username -e (to edit) -l(to list) -r(to remove) 10(minutes) 8-15(hours) *(Day of month) *(month) 1,3,5(days of week) /path/to/script/script_name.sh


              This will run your script once an hour from 8AM-3PM at 10 minutes past the hour every Monday, Wednesday and Friday.






              share|improve this answer

































                0














                First of All, we need to edit the crontab with Command crontab -eand than Inside this Crontab add the Path of Executable script and in your Case like this
                * 14 * * * home/hacks/notify.sh >/dev/null 2>&1 .



                Start /Stop / restart cron service




                • /etc/init.d/crond start /stop / restart

                • service crond start /stop /restart

                • systemctl stop crond.service


                systemctl stop crond.service






                share|improve this answer































                  -2














                  quite simple, add following line at bottom of the crontab file via:
                  sudo nano /etc/crontab



                  @reboot root cd /home/pi/node-sonos-http-api && npm start &






                  share|improve this answer


























                  • This does not seem to be an answer to the question.

                    – Ljm Dullaart
                    Dec 28 '18 at 19:00











                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "3"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f81262%2fhow-to-execute-shell-script-via-crontab%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  7 Answers
                  7






                  active

                  oldest

                  votes








                  7 Answers
                  7






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  38














                  Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script.



                  Make it look like this:



                  #!/bin/sh
                  notify-send "hi welcome"


                  and make sure the script is executable:



                  ls -l home/hacks/notify.sh
                  chmod +x home/hacks/notify.sh
                  ls -l home/hacks/notify.sh


                  Also, since you're asking for this to happen just once a day, is the timezone of the crontab the same as your own timezone? You might find this happening at 2pm GMT.






                  share|improve this answer
























                  • +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

                    – FXQuantTrader
                    Jan 9 '15 at 6:10











                  • Very subtle explanation. My upvote

                    – Fokwa Best
                    Oct 24 '16 at 12:58
















                  38














                  Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script.



                  Make it look like this:



                  #!/bin/sh
                  notify-send "hi welcome"


                  and make sure the script is executable:



                  ls -l home/hacks/notify.sh
                  chmod +x home/hacks/notify.sh
                  ls -l home/hacks/notify.sh


                  Also, since you're asking for this to happen just once a day, is the timezone of the crontab the same as your own timezone? You might find this happening at 2pm GMT.






                  share|improve this answer
























                  • +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

                    – FXQuantTrader
                    Jan 9 '15 at 6:10











                  • Very subtle explanation. My upvote

                    – Fokwa Best
                    Oct 24 '16 at 12:58














                  38












                  38








                  38







                  Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script.



                  Make it look like this:



                  #!/bin/sh
                  notify-send "hi welcome"


                  and make sure the script is executable:



                  ls -l home/hacks/notify.sh
                  chmod +x home/hacks/notify.sh
                  ls -l home/hacks/notify.sh


                  Also, since you're asking for this to happen just once a day, is the timezone of the crontab the same as your own timezone? You might find this happening at 2pm GMT.






                  share|improve this answer













                  Your script is missing a #! line at the start, which is the magic interpreted by the kernel to say which command interpreter is to be used for the script.



                  Make it look like this:



                  #!/bin/sh
                  notify-send "hi welcome"


                  and make sure the script is executable:



                  ls -l home/hacks/notify.sh
                  chmod +x home/hacks/notify.sh
                  ls -l home/hacks/notify.sh


                  Also, since you're asking for this to happen just once a day, is the timezone of the crontab the same as your own timezone? You might find this happening at 2pm GMT.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 10 '09 at 9:04









                  Phil PPhil P

                  1,75397




                  1,75397













                  • +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

                    – FXQuantTrader
                    Jan 9 '15 at 6:10











                  • Very subtle explanation. My upvote

                    – Fokwa Best
                    Oct 24 '16 at 12:58



















                  • +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

                    – FXQuantTrader
                    Jan 9 '15 at 6:10











                  • Very subtle explanation. My upvote

                    – Fokwa Best
                    Oct 24 '16 at 12:58

















                  +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

                  – FXQuantTrader
                  Jan 9 '15 at 6:10





                  +1 This answer is awesome - particularly noting the point about the script needs to being executable! Thanks!

                  – FXQuantTrader
                  Jan 9 '15 at 6:10













                  Very subtle explanation. My upvote

                  – Fokwa Best
                  Oct 24 '16 at 12:58





                  Very subtle explanation. My upvote

                  – Fokwa Best
                  Oct 24 '16 at 12:58













                  18














                  Making crontab running is easy only . Here I am going to say how to run crontab jobs. It is useful for anyone who is stuck on crontab.



                  */1 * * * * cd /home/hacks && sh notify.sh


                  To make the script executable, we have to do:



                  chmod +x home/hacks/notify.sh


                  Here i run this script for every one minute ...
                  By doing below script, you can write it in a log file to find whether its working



                  write log



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log


                  send mail



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log | mail -s "Hi this is example" user@domain.com





                  share|improve this answer





















                  • 2





                    isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

                    – user1179459
                    Sep 25 '15 at 1:14
















                  18














                  Making crontab running is easy only . Here I am going to say how to run crontab jobs. It is useful for anyone who is stuck on crontab.



                  */1 * * * * cd /home/hacks && sh notify.sh


                  To make the script executable, we have to do:



                  chmod +x home/hacks/notify.sh


                  Here i run this script for every one minute ...
                  By doing below script, you can write it in a log file to find whether its working



                  write log



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log


                  send mail



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log | mail -s "Hi this is example" user@domain.com





                  share|improve this answer





















                  • 2





                    isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

                    – user1179459
                    Sep 25 '15 at 1:14














                  18












                  18








                  18







                  Making crontab running is easy only . Here I am going to say how to run crontab jobs. It is useful for anyone who is stuck on crontab.



                  */1 * * * * cd /home/hacks && sh notify.sh


                  To make the script executable, we have to do:



                  chmod +x home/hacks/notify.sh


                  Here i run this script for every one minute ...
                  By doing below script, you can write it in a log file to find whether its working



                  write log



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log


                  send mail



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log | mail -s "Hi this is example" user@domain.com





                  share|improve this answer















                  Making crontab running is easy only . Here I am going to say how to run crontab jobs. It is useful for anyone who is stuck on crontab.



                  */1 * * * * cd /home/hacks && sh notify.sh


                  To make the script executable, we have to do:



                  chmod +x home/hacks/notify.sh


                  Here i run this script for every one minute ...
                  By doing below script, you can write it in a log file to find whether its working



                  write log



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log


                  send mail



                  */1 * * * * cd /home/hacks && sh notify.sh>>test.log | mail -s "Hi this is example" user@domain.com






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 28 '12 at 16:35









                  Community

                  1




                  1










                  answered Mar 25 '10 at 6:05









                  AravindAravind

                  291138




                  291138








                  • 2





                    isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

                    – user1179459
                    Sep 25 '15 at 1:14














                  • 2





                    isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

                    – user1179459
                    Sep 25 '15 at 1:14








                  2




                  2





                  isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

                  – user1179459
                  Sep 25 '15 at 1:14





                  isnt that "*/1 * * * * sh /home/hacks/notify.sh" will also work ?

                  – user1179459
                  Sep 25 '15 at 1:14











                  5














                  4 hypothesis:




                  • the cron daemon is not running (do a ps axfww | grep cron and check)


                  • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.


                  • your script is not executable


                  • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path







                  share|improve this answer






























                    5














                    4 hypothesis:




                    • the cron daemon is not running (do a ps axfww | grep cron and check)


                    • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.


                    • your script is not executable


                    • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path







                    share|improve this answer




























                      5












                      5








                      5







                      4 hypothesis:




                      • the cron daemon is not running (do a ps axfww | grep cron and check)


                      • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.


                      • your script is not executable


                      • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path







                      share|improve this answer















                      4 hypothesis:




                      • the cron daemon is not running (do a ps axfww | grep cron and check)


                      • the notify-send is trying to send output to a terminal, or an X session -- but it is ran from within the cron environment and it does not know "who to talk to", so to speak.


                      • your script is not executable


                      • the home/ path in the crontab script is relative to the user the scripts gets executed as. Try using the full path








                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Dec 10 '09 at 10:16

























                      answered Dec 10 '09 at 8:42









                      lorenzoglorenzog

                      1,62711223




                      1,62711223























                          1














                          Add export DISPLAY=:0 above the notify-send line in your script. This addresses lornezog's second point.






                          share|improve this answer




























                            1














                            Add export DISPLAY=:0 above the notify-send line in your script. This addresses lornezog's second point.






                            share|improve this answer


























                              1












                              1








                              1







                              Add export DISPLAY=:0 above the notify-send line in your script. This addresses lornezog's second point.






                              share|improve this answer













                              Add export DISPLAY=:0 above the notify-send line in your script. This addresses lornezog's second point.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 11 '12 at 0:33









                              W_WhalleyW_Whalley

                              3,06711115




                              3,06711115























                                  0














                                  You have to open crontab by the following command:



                                  crontab -u username -e (to edit) -l(to list) -r(to remove) 10(minutes) 8-15(hours) *(Day of month) *(month) 1,3,5(days of week) /path/to/script/script_name.sh


                                  This will run your script once an hour from 8AM-3PM at 10 minutes past the hour every Monday, Wednesday and Friday.






                                  share|improve this answer






























                                    0














                                    You have to open crontab by the following command:



                                    crontab -u username -e (to edit) -l(to list) -r(to remove) 10(minutes) 8-15(hours) *(Day of month) *(month) 1,3,5(days of week) /path/to/script/script_name.sh


                                    This will run your script once an hour from 8AM-3PM at 10 minutes past the hour every Monday, Wednesday and Friday.






                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      You have to open crontab by the following command:



                                      crontab -u username -e (to edit) -l(to list) -r(to remove) 10(minutes) 8-15(hours) *(Day of month) *(month) 1,3,5(days of week) /path/to/script/script_name.sh


                                      This will run your script once an hour from 8AM-3PM at 10 minutes past the hour every Monday, Wednesday and Friday.






                                      share|improve this answer















                                      You have to open crontab by the following command:



                                      crontab -u username -e (to edit) -l(to list) -r(to remove) 10(minutes) 8-15(hours) *(Day of month) *(month) 1,3,5(days of week) /path/to/script/script_name.sh


                                      This will run your script once an hour from 8AM-3PM at 10 minutes past the hour every Monday, Wednesday and Friday.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Jul 9 '11 at 1:47









                                      3498DB

                                      15.7k114762




                                      15.7k114762










                                      answered Dec 10 '09 at 10:00







                                      ravindrakhokharia






























                                          0














                                          First of All, we need to edit the crontab with Command crontab -eand than Inside this Crontab add the Path of Executable script and in your Case like this
                                          * 14 * * * home/hacks/notify.sh >/dev/null 2>&1 .



                                          Start /Stop / restart cron service




                                          • /etc/init.d/crond start /stop / restart

                                          • service crond start /stop /restart

                                          • systemctl stop crond.service


                                          systemctl stop crond.service






                                          share|improve this answer




























                                            0














                                            First of All, we need to edit the crontab with Command crontab -eand than Inside this Crontab add the Path of Executable script and in your Case like this
                                            * 14 * * * home/hacks/notify.sh >/dev/null 2>&1 .



                                            Start /Stop / restart cron service




                                            • /etc/init.d/crond start /stop / restart

                                            • service crond start /stop /restart

                                            • systemctl stop crond.service


                                            systemctl stop crond.service






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              First of All, we need to edit the crontab with Command crontab -eand than Inside this Crontab add the Path of Executable script and in your Case like this
                                              * 14 * * * home/hacks/notify.sh >/dev/null 2>&1 .



                                              Start /Stop / restart cron service




                                              • /etc/init.d/crond start /stop / restart

                                              • service crond start /stop /restart

                                              • systemctl stop crond.service


                                              systemctl stop crond.service






                                              share|improve this answer













                                              First of All, we need to edit the crontab with Command crontab -eand than Inside this Crontab add the Path of Executable script and in your Case like this
                                              * 14 * * * home/hacks/notify.sh >/dev/null 2>&1 .



                                              Start /Stop / restart cron service




                                              • /etc/init.d/crond start /stop / restart

                                              • service crond start /stop /restart

                                              • systemctl stop crond.service


                                              systemctl stop crond.service







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Oct 24 '15 at 12:00









                                              kunalkunal

                                              13




                                              13























                                                  -2














                                                  quite simple, add following line at bottom of the crontab file via:
                                                  sudo nano /etc/crontab



                                                  @reboot root cd /home/pi/node-sonos-http-api && npm start &






                                                  share|improve this answer


























                                                  • This does not seem to be an answer to the question.

                                                    – Ljm Dullaart
                                                    Dec 28 '18 at 19:00
















                                                  -2














                                                  quite simple, add following line at bottom of the crontab file via:
                                                  sudo nano /etc/crontab



                                                  @reboot root cd /home/pi/node-sonos-http-api && npm start &






                                                  share|improve this answer


























                                                  • This does not seem to be an answer to the question.

                                                    – Ljm Dullaart
                                                    Dec 28 '18 at 19:00














                                                  -2












                                                  -2








                                                  -2







                                                  quite simple, add following line at bottom of the crontab file via:
                                                  sudo nano /etc/crontab



                                                  @reboot root cd /home/pi/node-sonos-http-api && npm start &






                                                  share|improve this answer















                                                  quite simple, add following line at bottom of the crontab file via:
                                                  sudo nano /etc/crontab



                                                  @reboot root cd /home/pi/node-sonos-http-api && npm start &







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Dec 28 '18 at 16:32









                                                  Mureinik

                                                  2,54561625




                                                  2,54561625










                                                  answered Dec 28 '18 at 16:01









                                                  Martini7Martini7

                                                  1




                                                  1













                                                  • This does not seem to be an answer to the question.

                                                    – Ljm Dullaart
                                                    Dec 28 '18 at 19:00



















                                                  • This does not seem to be an answer to the question.

                                                    – Ljm Dullaart
                                                    Dec 28 '18 at 19:00

















                                                  This does not seem to be an answer to the question.

                                                  – Ljm Dullaart
                                                  Dec 28 '18 at 19:00





                                                  This does not seem to be an answer to the question.

                                                  – Ljm Dullaart
                                                  Dec 28 '18 at 19:00


















                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Super User!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f81262%2fhow-to-execute-shell-script-via-crontab%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Сан-Квентин

                                                  8-я гвардейская общевойсковая армия

                                                  Алькесар