Getting script to run on boot up in linux

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Getting script to run on boot up in linux

 
0
  #1
Oct 10th, 2009
Hi guys,

I've come to a road block so i need some help.

1)I have an exe which i need to run at start up.

So I put the following bash file in the inid.d directory:


Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2. #
  3. #
  4.  
  5. . /etc/rc.d/init.d/functions
  6.  
  7. EXE_DIR="/root/monkey-0.9.2/bin/"
  8. EXE_NAME="monkey"
  9.  
  10.  
  11. #############################################################
  12. #################### Functions ##############################
  13. #############################################################
  14.  
  15. function start {
  16. RUNNING=`ps -ef | grep -c ./${EXE_NAME}\$`
  17. if [ $RUNNING -ge 1 ]; then
  18. echo "${EXE_NAME} is already running"
  19. echo_failure
  20. echo ""
  21. else
  22. if [ ! -x "${EXE_DIR}${EXE_NAME}" ]; then
  23. echo ""
  24. echo "[error] ${EXE_NAME} doesn't exist"
  25. echo_failure
  26. echo ""
  27. else
  28. cd "${EXE_DIR}"
  29. sleep 1
  30. ./${EXE_NAME} > output &
  31. disown
  32. echo "Restarting ${EXE_NAME}"
  33. echo_ok
  34. echo ""
  35. fi
  36. fi
  37. }
  38.  
  39. function stop {
  40. echo ""
  41. echo "Stopping ${EXE_NAME}"
  42. RUNNING=`ps -ef | grep -c ./${EXE_NAME}\$`
  43. if [ $RUNNING -ge 1 ]; then
  44. ID=`ps -ef | grep ${EXE_NAME} | grep -v grep | awk '{print $2}'`
  45. echo "PID= $ID"
  46. kill -9 $ID
  47. echo_ok
  48. echo ""
  49. else
  50. echo "${EXE_NAME} is not running"
  51. echo_failure
  52. echo ""
  53. fi
  54.  
  55.  
  56. return 0;
  57. }
  58.  
  59. function restart {
  60. stop
  61. start
  62. return 1;
  63. }
  64.  
  65. function status {
  66. RUNNING=`ps -ef | grep -c ./${EXE_NAME}\$`
  67. if [ $RUNNING -ge 1 ]; then
  68. echo "${EXE_NAME} Running"
  69. echo_ok
  70. echo ""
  71. tail -f "${EXE_DIR}output"
  72. else
  73. echo "${EXE_NAME} Not Running"
  74. echo_failure
  75. echo ""
  76. fi
  77. }
  78.  
  79. #############################################################
  80. ###################### MAIN #################################
  81. #############################################################
  82.  
  83.  
  84. case "$1" in
  85. start)
  86. start
  87. ;;
  88.  
  89. stop)
  90. stop
  91. ;;
  92.  
  93. restart)
  94. restart
  95. ;;
  96.  
  97. details|status)
  98. status
  99. ;;
  100.  
  101. *)
  102.  
  103. exit 1
  104. esac
  105.  
  106. exit 0

The funny thing is the pid says it is running on boot. But it only works when I log on and restart the program manually?!

Any ideas?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
1
  #2
Oct 10th, 2009
What do you mean "The funny thing is the pid says it is running on boot"? Scripts in init.d must be referenced from their run levels. Depending on your distro you should have a command for update-rc.d to specify the run levels to execute the scripts.

Also the symlink naming convention for executing a process in the rc?.d directories is important. For example if monkey logs to syslog but starts before syslog is running then you may have a problem. HTTPDs are typically one of the last processes to start.

[edit]
One more thing .. usually scripts use pidfiles in /var/run . There are a lot of situations where ps may not return what you expect. ie a user has a process start up with your exe's name which will cause your script to think it is already running, regardless of its current state.
[/edit]
Last edited by sknake; Oct 10th, 2009 at 10:17 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #3
Oct 10th, 2009
>What do you mean "The funny thing is the pid says it is running on boot"?

Well the program (c++) is actually running on start up. You see it is mini light-weight webserver.

http://www.monkey-project.com/

However, when I go to the web page it comes up with the 404 page not found error. But when I restart the program manually from the terminal, it works fine.

>Depending on your distro you should have a command for update-rc.d to specify the run levels to execute the scripts.

At the moment I'm running it off a itx board that was compiled from scratch. I didn't do it so I'm kinda clueless. But if I can get this working in ubuntu for example it would be a start.

Thanx in advance.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
Oct 10th, 2009
Post the output for this sk:/etc# ls -al rc?.d/ Your directory structure appears to be a little different in /etc so you may need to find where the rc?.d's are located: sk:/etc# find ./ -iname rc\*\.d
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #5
Oct 10th, 2009
Here is how to add a script in debian (more or less the same in ubuntu, which is debian based):

http://74.125.47.132/search?q=cache:...&ct=clnk&gl=us

[edit]
/etc/init.d/skeleton contains a template for adding new startup scripts. You should be able to modify it to your needs
[/edit]
Last edited by sknake; Oct 10th, 2009 at 10:31 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #6
Oct 10th, 2009
Here is my output:

Shell Scripting Syntax (Toggle Plain Text)
  1. root@localhost:/etc# ls -al rc.d/
  2. total 44
  3. drwxr-xr-x 11 root root 4096 Oct 6 2008 .
  4. drwxr-xr-x 13 root root 4096 Jan 1 00:00 ..
  5. drwxr-xr-x 2 root root 4096 Aug 14 2009 init.d
  6. drwxr-xr-x 2 root root 4096 Aug 10 2009 rc0.d
  7. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc1.d
  8. drwxr-xr-x 2 root root 4096 Nov 24 2008 rc2.d
  9. drwxr-xr-x 2 root root 4096 Aug 14 2009 rc3.d
  10. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc4.d
  11. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc5.d
  12. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc6.d
  13. drwxr-xr-x 2 root root 4096 Oct 6 2008 rcsysinit.d
  14. root@localhost:/etc# drwxr-xr-x 11 root root 4096 Oct 6 2008 .
  15. -bash: drwxr-xr-x: command not found
  16. root@localhost:/etc# drwxr-xr-x 13 root root 4096 Jan 1 00:00 ..
  17. -bash: drwxr-xr-x: command not found
  18. drwxr-xr-x 2 root root 4096 Aug 10 2009 rc0.d
  19. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc1.d
  20. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Aug 14 2009 init.d
  21. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc4.d
  22. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc5.d
  23. -bash: drwxr-xr-x: command not found
  24. drwxr-xr-x 2 root root 4096 Oct 6 2008 rc6.d
  25. drwxr-xr-x 2 root root 4096 Oct 6 2008 rcsysinit.d
  26. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Aug 10 2009 rc0.d
  27. -bash: drwxr-xr-x: command not found
  28. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Oct 6 2008 rc1.d
  29. -bash: drwxr-xr-x: command not found
  30. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Nov 24 2008 rc2.d
  31. -bash: drwxr-xr-x: command not found
  32. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Aug 14 2009 rc3.d
  33. -bash: drwxr-xr-x: command not found
  34. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Oct 6 2008 rc4.d
  35. -bash: drwxr-xr-x: command not found
  36. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Oct 6 2008 rc5.d
  37. -bash: drwxr-xr-x: command not found
  38. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Oct 6 2008 rc6.d
  39. -bash: drwxr-xr-x: command not found
  40. root@localhost:/etc# drwxr-xr-x 2 root root 4096 Oct 6 2008 rcsysinit.d
  41. -bash: drwxr-xr-x: command not found
  42. root@localhost:/etc#
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #7
Oct 10th, 2009
eh that wasn't the command I posted. ls -al rc?.d/ the ? expands for rc0.d/ rc1.d/ to list the directory contents, such as:

  1. sk:/etc# ls -al rc?.d | less
  2. rc0.d:
  3. total 16
  4. drwxr-xr-x 2 root root 4096 Mar 31 2007 .
  5. drwxr-x--x 129 root root 8192 Aug 17 18:30 ..
  6. lrwxrwxrwx 1 root root 17 Feb 12 2007 K09apache2 -> ../init.d/apache2
  7. lrwxrwxrwx 1 root root 14 Oct 15 2004 K11cron -> ../init.d/cron
  8. lrwxrwxrwx 1 root root 19 Oct 15 2004 K19setserial -> ../init.d/setserial
  9. lrwxrwxrwx 1 root root 14 Feb 8 2005 K20acct -> ../init.d/acct
  10. lrwxrwxrwx 1 root root 20 Dec 1 2005 K20bittorrent -> ../init.d/bittorrent
  11. lrwxrwxrwx 1 root root 17 Dec 19 2004 K20caudium -> ../init.d/caudium
  12. lrwxrwxrwx 1 root root 28 Oct 19 2004 K20courier-authdaemon -> ../init.d/courier-authdaemon
  13. lrwxrwxrwx 1 root root 22 Oct 19 2004 K20courier-imap -> ../init.d/courier-imap
  14. lrwxrwxrwx 1 root root 26 Oct 19 2004 K20courier-imap-ssl -> ../init.d/courier-imap-ssl
  15. lrwxrwxrwx 1 root root 21 Nov 23 2004 K20courier-pop -> ../init.d/courier-pop
  16. lrwxrwxrwx 1 root root 25 Nov 23 2004 K20courier-pop-ssl -> ../init.d/courier-pop-ssl
  17. lrwxrwxrwx 1 root root 16 Feb 25 2005 K20distcc -> ../init.d/distcc
  18. lrwxrwxrwx 1 root root 13 Mar 7 2007 K20gpm -> ../init.d/gpm
  19. lrwxrwxrwx 1 root root 17 Nov 24 2004 K20mailman -> ../init.d/mailman
  20. lrwxrwxrwx 1 root root 17 Oct 15 2004 K20makedev -> ../init.d/makedev
  21. lrwxrwxrwx 1 root root 15 Jul 14 2005 K20mbmon -> ../init.d/mbmon
  22. lrwxrwxrwx 1 root root 17 Oct 15 2004 K20oidentd -> ../init.d/oidentd
  23. lrwxrwxrwx 1 root root 23 Feb 26 2007 K20openbsd-inetd -> ../init.d/openbsd-inetd
  24. lrwxrwxrwx 1 root root 19 Feb 21 2005 K20pure-ftpd -> ../init.d/pure-ftpd
  25. lrwxrwxrwx 1 root root 15 Oct 18 2004 K20qmail -> ../init.d/qmail
  26. lrwxrwxrwx 1 root root 16 Nov 22 2004 K20quagga -> ../init.d/quagga
  27. lrwxrwxrwx 1 root root 15 Mar 18 2006 K20rsync -> ../init.d/rsync
  28. lrwxrwxrwx 1 root root 15 Oct 23 2004 K20snmpd -> ../init.d/snmpd
  29. lrwxrwxrwx 1 root root 13 Oct 15 2004 K20ssh -> ../init.d/ssh
  30. lrwxrwxrwx 1 root root 17 Mar 12 2007 K20sysstat -> ../init.d/sysstat
  31. lrwxrwxrwx 1 root root 16 Oct 23 2004 K20webmin -> ../init.d/webmin
  32. lrwxrwxrwx 1 root root 22 Nov 23 2004 K21spamassassin -> ../init.d/spamassassin
  33. lrwxrwxrwx 1 root root 13 Feb 1 2007 K23ntp -> ../init.d/ntp
  34. lrwxrwxrwx 1 root root 20 Feb 22 2007 K25hwclock.sh -> ../init.d/hwclock.sh
  35. lrwxrwxrwx 1 root root 23 Oct 15 2004 K30etc-setserial -> ../init.d/etc-setserial
  36. lrwxrwxrwx 1 root root 18 Jun 20 2005 K40arpwatch -> ../init.d/arpwatch
  37. lrwxrwxrwx 1 root root 16 Nov 22 2004 K75hdparm -> ../init.d/hdparm
  38. lrwxrwxrwx 1 root root 18 Jan 25 2005 K79quotarpc -> ../init.d/quotarpc
  39. lrwxrwxrwx 1 root root 15 Oct 16 2004 K85bind9 -> ../init.d/bind9
  40. lrwxrwxrwx 1 root root 15 Jan 25 2005 K85quota -> ../init.d/quota
  41. lrwxrwxrwx 1 root root 13 Nov 27 2004 K89atd -> ../init.d/atd
  42. lrwxrwxrwx 1 root root 15 Oct 15 2004 K89klogd -> ../init.d/klogd
  43. lrwxrwxrwx 1 root root 18 Oct 15 2004 K90sysklogd -> ../init.d/sysklogd
  44. lrwxrwxrwx 1 root root 16 Oct 23 2004 K91apache -> ../init.d/apache
  45. lrwxrwxrwx 1 root root 20 Feb 27 2005 K91apache-ssl -> ../init.d/apache-ssl
  46. lrwxrwxrwx 1 root root 12 Oct 23 2004 K99ud -> ../init.d/ud
  47. -rw-r--r-- 1 root root 355 Jan 7 2006 README
  48. lrwxrwxrwx 1 root root 18 Feb 1 2007 S20sendsigs -> ../init.d/sendsigs
  49. lrwxrwxrwx 1 root root 17 Oct 15 2004 S30urandom -> ../init.d/urandom
  50. lrwxrwxrwx 1 root root 22 Oct 15 2004 S31umountnfs.sh -> ../init.d/umountnfs.sh
  51. lrwxrwxrwx 1 root root 20 Oct 15 2004 S35networking -> ../init.d/networking
  52. lrwxrwxrwx 1 root root 18 Oct 15 2004 S40umountfs -> ../init.d/umountfs
  53. lrwxrwxrwx 1 root root 20 Sep 12 2005 S60umountroot -> ../init.d/umountroot
  54. lrwxrwxrwx 1 root root 14 Oct 15 2004 S90halt -> ../init.d/halt
  55.  
  56. rc1.d:
  57. total 16
  58. drwxr-xr-x 2 root root 4096 Mar 31 2007 .
  59. drwxr-x--x 129 root root 8192 Aug 17 18:30 ..
  60. lrwxrwxrwx 1 root root 17 Feb 12 2007 K09apache2 -> ../init.d/apache2
  61. lrwxrwxrwx 1 root root 14 Oct 15 2004 K11cron -> ../init.d/cron
  62. lrwxrwxrwx 1 root root 14 Feb 8 2005 K20acct -> ../init.d/acct
  63. lrwxrwxrwx 1 root root 17 Dec 19 2004 K20caudium -> ../init.d/caudium
  64. lrwxrwxrwx 1 root root 28 Oct 19 2004 K20courier-authdaemon -> ../init.d/courier-authdaemon
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #8
Oct 10th, 2009
I am also going to attach the web-server files as their hosting seems to be down.
Attached Files
File Type: zip monkey-0.9.2.zip (127.2 KB, 2 views)
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #9
Oct 10th, 2009
Oh right not sure, I'm completely lost when it comes to shell scripting and linux

Shell Scripting Syntax (Toggle Plain Text)
  1. root@localhost:/etc# find ./ -iname rc\*\.d
  2. ./rc.d
  3. ./rc.d/rc6.d
  4. ./rc.d/rc2.d
  5. ./rc.d/rc0.d
  6. ./rc.d/rcsysinit.d
  7. ./rc.d/rc1.d
  8. ./rc.d/rc5.d
  9. ./rc.d/rc3.d
  10. ./rc.d/rc4.d
  11. root@localhost:/etc#
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,410
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 614
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #10
Oct 10th, 2009
Ok -- now try this:
ls -alR /etc/rc.d/rc?.d
It will be a lot of output so you have two options
1) Write it to a file: ls -alR /etc/rc.d/rc?.d >> /tmp/outfile
2) Use a pager: ls -alR /etc/rc.d/rc?.d | less or ls -alR /etc/rc.d/rc?.d | more . Your system may not have one of the pagers installer. In my opinion less > more, but either will work.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Shell Scripting
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC