{"id":76,"date":"2014-01-14T10:11:06","date_gmt":"2014-01-14T10:11:06","guid":{"rendered":"http:\/\/queen.run.montefiore.ulg.ac.be\/~barbette\/?p=76"},"modified":"2015-02-05T14:52:33","modified_gmt":"2015-02-05T13:52:33","slug":"detecting-if-another-user-is-connected","status":"publish","type":"post","link":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/","title":{"rendered":"Detecting if another user is connected on UNIX systems"},"content":{"rendered":"<p>How to detect if another user is connected to your machine or your server? You can use the command &#8220;users&#8221; to check yourself is someone is connected. But to do it automatically, you&#8217;ll have to use some pipe :<\/p>\n<p>[code lang=&#8221;bash&#8221;]expr length &#8220;`users | sed -e &#8220;s\/\\($USER\\|\\[ \\]*\\)\/\/ig&#8221;`&#8221;[\/code]<\/p>\n<p>The first thing executed by the shell will be the thing under french apostrophe ( ` ). Theses are for evaluation a command, and replace it by what it outputs (normally print on the screen). The command users print the list of connected users into a pipe, to sed which evaluate its command as a regular expression (-e parameter). The command is &#8220;s&#8221; for substitute, and the rest tells him to find &#8220;$USER&#8221; (replaced by the currently connected user, you) and spaces and replace them by &#8230; nothing. So this part will be an empty string if there is no other users connected than &#8220;$USER&#8221; and something not empty if there is.<\/p>\n<p>The &#8220;expr length&#8221; return the length of a string. So this commands print 0 if there is no other user connected, and &gt;0 if there is some !<\/p>\n<p>In a shell script to do something if yes or no&#8230;<br \/>\n[code lang=&#8221;bash&#8221;]#!\/bin\/sh<br \/>\nusersstripped=`users | sed -e &#8220;s\/\\(<strong>tom<\/strong>\\|\\[ \\]*\\)\/\/ig&#8221;`<br \/>\nconnected=`expr length &#8220;$usersstripped&#8221;`<br \/>\nif [ &#8220;$connected&#8221; -eq &#8220;0&#8221; ] ; then<br \/>\necho &#8220;No other user is connected&#8221;<br \/>\nelse<br \/>\necho &#8220;Other user connected !&#8221;<br \/>\nfi[\/code]<\/p>\n<p>It is essentially the same command but done in two times, as in a shell script this command would not be evaluated correctly.<\/p>\n<p>And if you want to put that in a cron to eg. send you a mail, you just have to type &#8220;crontab -e&#8221; and put a line like :<\/p>\n<p>[code]* * * * * \/home\/tom\/connected[\/code]<\/p>\n<p>To launch it every minutes. But if you do that you&#8217;d better do something like detecting a connection&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to detect if another user is connected to your machine or your server? You can use the command &#8220;users&#8221; to check yourself is someone is connected. But to do it automatically, you&#8217;ll have to use some pipe : [code lang=&#8221;bash&#8221;]expr length &#8220;`users | sed -e &#8220;s\/\\($USER\\|\\[ \\]*\\)\/\/ig&#8221;`&#8221;[\/code] The first thing executed by the shell &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Detecting if another user is connected on UNIX systems&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[24,19,22,21,20,23],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-unix","tag-connected","tag-linux","tag-sed","tag-shell","tag-unix-2","tag-users"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Detecting if another user is connected on UNIX systems - Tom Barbette<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Detecting if another user is connected on UNIX systems - Tom Barbette\" \/>\n<meta property=\"og:description\" content=\"How to detect if another user is connected to your machine or your server? You can use the command &#8220;users&#8221; to check yourself is someone is connected. But to do it automatically, you&#8217;ll have to use some pipe : [code lang=&#8221;bash&#8221;]expr length &#8220;`users | sed -e &#8220;s\/($USER|[ ]*)\/\/ig&#8221;`&#8221;[\/code] The first thing executed by the shell &hellip; Continue reading &quot;Detecting if another user is connected on UNIX systems&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/\" \/>\n<meta property=\"og:site_name\" content=\"Tom Barbette\" \/>\n<meta property=\"article:published_time\" content=\"2014-01-14T10:11:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-02-05T13:52:33+00:00\" \/>\n<meta name=\"author\" content=\"Tom Barbette\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TomBarbette\" \/>\n<meta name=\"twitter:site\" content=\"@TomBarbette\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Tom Barbette\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/\"},\"author\":{\"name\":\"Tom Barbette\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#\\\/schema\\\/person\\\/7f791766092e1207bc7a94a65e74f4fd\"},\"headline\":\"Detecting if another user is connected on UNIX systems\",\"datePublished\":\"2014-01-14T10:11:06+00:00\",\"dateModified\":\"2015-02-05T13:52:33+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/\"},\"wordCount\":313,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#organization\"},\"keywords\":[\"connected\",\"linux\",\"sed\",\"shell\",\"unix\",\"users\"],\"articleSection\":[\"Unix\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/\",\"url\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/\",\"name\":\"Detecting if another user is connected on UNIX systems - Tom Barbette\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#website\"},\"datePublished\":\"2014-01-14T10:11:06+00:00\",\"dateModified\":\"2015-02-05T13:52:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/detecting-if-another-user-is-connected\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Detecting if another user is connected on UNIX systems\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#website\",\"url\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/\",\"name\":\"Tom Barbette\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#organization\",\"name\":\"Efficiency of Networked Systems Group\",\"url\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/wp-content\\\/uploads\\\/2022\\\/04\\\/cropped-logo-uclouvain-2021-barbette.png\",\"contentUrl\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/wp-content\\\/uploads\\\/2022\\\/04\\\/cropped-logo-uclouvain-2021-barbette.png\",\"width\":512,\"height\":512,\"caption\":\"Efficiency of Networked Systems Group\"},\"image\":{\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/x.com\\\/TomBarbette\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/tom-barbette\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/#\\\/schema\\\/person\\\/7f791766092e1207bc7a94a65e74f4fd\",\"name\":\"Tom Barbette\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aecd99cba3b4d91d3775fde3219bfa362cd99acfbe95848ec303b9685f366fcd?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aecd99cba3b4d91d3775fde3219bfa362cd99acfbe95848ec303b9685f366fcd?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/aecd99cba3b4d91d3775fde3219bfa362cd99acfbe95848ec303b9685f366fcd?s=96&r=g\",\"caption\":\"Tom Barbette\"},\"sameAs\":[\"https:\\\/\\\/www.tombarbette.be\"],\"url\":\"https:\\\/\\\/perso.uclouvain.be\\\/tom.barbette\\\/author\\\/tom\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Detecting if another user is connected on UNIX systems - Tom Barbette","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/","og_locale":"en_US","og_type":"article","og_title":"Detecting if another user is connected on UNIX systems - Tom Barbette","og_description":"How to detect if another user is connected to your machine or your server? You can use the command &#8220;users&#8221; to check yourself is someone is connected. But to do it automatically, you&#8217;ll have to use some pipe : [code lang=&#8221;bash&#8221;]expr length &#8220;`users | sed -e &#8220;s\/($USER|[ ]*)\/\/ig&#8221;`&#8221;[\/code] The first thing executed by the shell &hellip; Continue reading \"Detecting if another user is connected on UNIX systems\"","og_url":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/","og_site_name":"Tom Barbette","article_published_time":"2014-01-14T10:11:06+00:00","article_modified_time":"2015-02-05T13:52:33+00:00","author":"Tom Barbette","twitter_card":"summary_large_image","twitter_creator":"@TomBarbette","twitter_site":"@TomBarbette","twitter_misc":{"Written by":"Tom Barbette","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/#article","isPartOf":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/"},"author":{"name":"Tom Barbette","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#\/schema\/person\/7f791766092e1207bc7a94a65e74f4fd"},"headline":"Detecting if another user is connected on UNIX systems","datePublished":"2014-01-14T10:11:06+00:00","dateModified":"2015-02-05T13:52:33+00:00","mainEntityOfPage":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/"},"wordCount":313,"commentCount":0,"publisher":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#organization"},"keywords":["connected","linux","sed","shell","unix","users"],"articleSection":["Unix"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/","url":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/","name":"Detecting if another user is connected on UNIX systems - Tom Barbette","isPartOf":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#website"},"datePublished":"2014-01-14T10:11:06+00:00","dateModified":"2015-02-05T13:52:33+00:00","breadcrumb":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/detecting-if-another-user-is-connected\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/perso.uclouvain.be\/tom.barbette\/"},{"@type":"ListItem","position":2,"name":"Detecting if another user is connected on UNIX systems"}]},{"@type":"WebSite","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#website","url":"https:\/\/perso.uclouvain.be\/tom.barbette\/","name":"Tom Barbette","description":"","publisher":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/perso.uclouvain.be\/tom.barbette\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#organization","name":"Efficiency of Networked Systems Group","url":"https:\/\/perso.uclouvain.be\/tom.barbette\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#\/schema\/logo\/image\/","url":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-content\/uploads\/2022\/04\/cropped-logo-uclouvain-2021-barbette.png","contentUrl":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-content\/uploads\/2022\/04\/cropped-logo-uclouvain-2021-barbette.png","width":512,"height":512,"caption":"Efficiency of Networked Systems Group"},"image":{"@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/TomBarbette","https:\/\/www.linkedin.com\/in\/tom-barbette"]},{"@type":"Person","@id":"https:\/\/perso.uclouvain.be\/tom.barbette\/#\/schema\/person\/7f791766092e1207bc7a94a65e74f4fd","name":"Tom Barbette","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/aecd99cba3b4d91d3775fde3219bfa362cd99acfbe95848ec303b9685f366fcd?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/aecd99cba3b4d91d3775fde3219bfa362cd99acfbe95848ec303b9685f366fcd?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/aecd99cba3b4d91d3775fde3219bfa362cd99acfbe95848ec303b9685f366fcd?s=96&r=g","caption":"Tom Barbette"},"sameAs":["https:\/\/www.tombarbette.be"],"url":"https:\/\/perso.uclouvain.be\/tom.barbette\/author\/tom\/"}]}},"_links":{"self":[{"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":5,"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":393,"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/posts\/76\/revisions\/393"}],"wp:attachment":[{"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perso.uclouvain.be\/tom.barbette\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}