24 ms·
Ask HN: How to start a headless process and logout, continues?
The ultimate thing I'm working on is scraping and storing into database/display into a web application. This is a personal project, I just want something that runs and when I get home I have new things to look at sort of thing.
I can do this from the perspective of a timed event with a web page open using JavaScript/AJAX and PHP/MYSQL. I'll keep the PHP/MYSQL part.
I tried to work with CRON but it didn't work out well.
I recently bought a raspberry pi zero and setup a socket-server so to speak with a usb wifi adapter. So I want to be able to plug that into a wall, and then ssh into it to start a process, then that runs on its own. I'm just not sure what I'm looking for here, where I can have something trigger PHP but the process won't end when I log out of SSH.
I've primarily (pretty much only) used a LAMP setup and haven't done anything server side that involves processes. Usually everything is triggered by a client-facing thing. Whether it's asynchronous loading or a get/post. Not something that runs on its own outside of /var/www/html (public)
I did come across CGI maybe that's what I'm looking for. I've also seen that you can communicate with a MYSQL library with C++ I think last time I looked. It's just that I'm used to using the PHP htmlsimpledom scraper.
I've been listening to a lot of podcasts talking about Go maybe that's something but definitely outside of my scope at this time.
- CyberFonic 10y agoThe nohup approach with logging is the *nix way of doing ad-hoc stuff. You could also look at mosh.org, it works like ssh, but handles disconnects and re-connects well. You could even ^Z; bg to make your program run in background and when you reconnect, you'd just do a fg to get it back into the foreground. Since your shell doesn't get logged out, you'd be Ok.
- ge96 10y agoDang I have to admit I had to look up a lot of words that you mentioned. One concern too is the ability to terminate it if it's essentially a continuous poll/interval. Something is wrong, need to pass in a false variable to stop the loop. Thanks, will check this out.
- JoshTriplett 10y agoOn the command line, take a look at "nohup". Programmatically, read about how to "daemonize" (which involves calling fork() twice). And if you actually want to run a server more persistently (not just keep a process running that you launched from SSH), modern init systems (such as systemd) make that easy to do by handling daemonization for you, such that your code doesn't need to do anything special.
- ge96 10y agoCool, thanks a lot at least I have a direction to look. Yeah it's just odd to me since I haven't done something like this before, how you keep something running that isn't on the public/client-triggered side. edit: Actually a while back I did do a socket.io tutorial where you triggered the process by terminal. Not sure if directly related, node was mentioned below. I just wonder if you left the terminal would it end. Obviously not I don't think since this is in practice now on the web and I doubt they just have terminals open to keep a process running.
- onion2k 10y agoTrying to use a repeating timer event in a webpage to call a PHP script is a pretty bad idea. It'd be horribly fragile. Your main options are really either cron (which is a pain but once you get it working you're fine) or a long-running process. If you really don't want to use cron then nodejs or python would be pretty solid choices.
- ge96 10y agoIt's not that I don't want to use CRON, I just couldn't get it to work when I tried. I tried with Ubuntu (root problem), then I tried debian (no root problem) but it wouldn't execute. I think I did look at the dmesg or something logs I can't remember the reason but yeah it wouldn't execute my test script. Haven't touched nodejs or python but I am open to it. Thanks. I'm alright with JavaScript (I can build things that I want in general) so maybe Node. Python the syntax I'm not familiar (have to practice/use it) Anyway thanks for the info