0

I want to run the script every 30 minutes with cron but I have problem with my code. In every 30 min I have to kill old script and run it again. I have somethink like this, but it is not working:

    cd /var/www/scripts
    pkill -f bot
    now="$(date +%Y%m%d%H%M%S)"
    screen -S bot 
    node mybot.js >> logi/logi_$now.txt
Jensej
  • 1,255
  • 6
  • 21
  • 33
  • 2
    In what way is it not working? What do you expect it to do and what does it do instead? – das-g Oct 10 '15 at 10:44
  • node mybot.js >> logi/logi_$now.txt this line is not working, script has stopped in screen -S bot – Jensej Oct 10 '15 at 10:55

3 Answers3

1

use crontab :

 crontab -l

*/30 * * * * /path/to/your/command

save and run

shashank
  • 1,133
  • 2
  • 10
  • 24
1

You may not use screen for running things in background in a script. Use ampersand (&) to background a process and nohup so it won't be killed when cron script exits. Also remember a subprocess PID in a file.

Something like this:

kill -- "$(cat mybot.pid)"
now="$(date +%Y%m%d%H%M%S)"
nohup node mybot.js >> "logi/logi_$now.txt" &
echo $! > mybot.pid
Tometzky
  • 22,573
  • 5
  • 59
  • 73
0

The line

    node mybot.js >> logi/logi_$now.txt

is never reached, as screen -S <session name> will start a screen session and therefore a new shell and connect to it. The rest of the script would only execute once that 'inner' session terminates.

screen is more for interactive use. Calling it in a script like this is rather strange. I guess you want to have node mybot.js >> logi/logi_$now.txt running in the background, so that your script can terminate while node keeps running. See Redirecting stdout & stderr from background process and Node.js as a background service for options how to do that.

Community
  • 1
  • 1
das-g
  • 9,718
  • 4
  • 38
  • 80